Git Product home page Git Product logo

csb's Introduction

Gem Version Build

Csb

A simple and streaming support CSV template engine for Ruby on Rails.

Features

  • Support for streaming downloads
  • Output in UTF-8 with BOM
  • Readable code
  • High testability

Usage

Template handler

In app/controllers/reports_controller.rb:

def index
  @reports = Report.preload(:categories)
end

In app/views/reports/index.csv.csb:

csv.items = @reports

# When there are many records
# csv.items = @reports.find_each

# When there are many records with decorator
# csv.items = @reports.find_each.lazy.map(&:decorate)

# csv.filename = "reports_#{Time.current.to_i}.csv"
# csv.streaming = false

csv.cols.add('Update date') { |r| l(r.updated_at.to_date) }
csv.cols.add('Categories') { |r| r.categories.pluck(:name).join(' ') }
csv.cols.add('Content', :content)
csv.cols.add('Empty')
csv.cols.add('Static', 'dummy')

Output:

Update date,Categories,Content,Empty,Static
2019/06/01,category1 category2,content1,,dummy
2019/06/02,category3,content2,,dummy

Directly

csv = Csb::Builder.new(items: items)
csv.cols.add('Update date') { |r| l(r.updated_at.to_date) }
csv.cols.add('Categories') { |r| r.categories.pluck(:name).join(' ') }
csv.cols.add('Content', :content)
csv.cols.add('Empty')
csv.cols.add('Static', 'dummy')
csv.build

# =>
# Update date,Categories,Content,Empty,Static
# 2019/06/01,category1 category2,content1,,dummy
# 2019/06/02,category3,content2,,dummy

Testing

# Your view
csv.items = @articles
csv.cols = Article.csb_cols

# Your Model
def self.csb_cols
  Csb::Cols.new do |cols|
    cols.add('Update date') { |r| I18n.l(r.updated_at.to_date) }
    cols.add('Categories') { |r| r.categories.pluck(:name).join(' ') }
    cols.add('Title', :title)
  end
end

# Your test
require 'csb/testing'

expect(Article.csb_cols.col_pairs(article)).to eq [
  ['Update date', '2020-01-01'],
  ['Categories', 'test rspec'],
  ['Title', 'Testing'],
]

expect(Article.csb_cols.as_table(articles)).to eq [
  ['Update date', 'Categories', 'Title'],
  ['2020-01-01', 'test rspec', 'Testing'],
  ['2020-02-01', 'rails gem', 'Rails 6.2'],
]

Installation

Add this line to your application's Gemfile:

gem 'csb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install csb

Configuration

In config/initializers/csb.rb, you can configure the following values.

Csb.configure do |config|
  config.utf8_bom = true # default: false
  config.streaming = false # default: true
  config.after_streaming_error = ->(error) do # default: nil
    Rails.logger.error(error)
    Bugsnag.notify(error)
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/aki77/csb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Csb project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

csb's People

Contributors

aki77 avatar taketo1113 avatar petergoldstein avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.