rails_csv_renderer 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 660d1de18d4f98f836f8ece40ad26cfa0a39a075
4
- data.tar.gz: 71d8cf91962e28da4c43efee012d2720db554217
3
+ metadata.gz: be2a02e163df9e40eb13fc2a2f84f4037d1fb1bb
4
+ data.tar.gz: 2995b56dbdfb98207f1ac9bfb9499e06813b3085
5
5
  SHA512:
6
- metadata.gz: 874f2818485fe8e655fb0c5d4b161407b7e837777e47431272271a698b9d89d2c43292908b69b7d92dfa9699fcbe8d7f52b9124647344fca52433c4903e76cab
7
- data.tar.gz: 032513b1c958d5d2effcec6732fc1ea66fc1f0836a5fcae397673531a8bc934095e39caf37d994ae63f733aa9b485faab92396c1ae5c8e56374a30c21eecf68a
6
+ metadata.gz: e0c9a2506b68e27c5a67f17020c8a3b520319eb502fab0a7820233bc48c8641b8ddd95d35adb78411cd9eae6c2fa2003e061701c1304eba16a968679f572ad34
7
+ data.tar.gz: 692e3b63fdfb9305055158a248b171edc0cfe134277a68dceba8d38f544129f88d1ba3fa846be98b48d55967e9d14ce70eb3c7e270f60335556c13f6c2c9920f
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ ## 0.2.1
4
+
5
+ * Fix: init renderer
6
+ * Add :columns option
7
+
8
+ ## 0.1.0
9
+
10
+ * Initial Release
data/README.md CHANGED
@@ -52,6 +52,28 @@ Will render a CSV file similar to:
52
52
  </tr>
53
53
  </table>
54
54
 
55
+ ### Options
56
+
57
+ You can pass few options at call of rendrer:
58
+
59
+ * *:filename* - Name of file. Optional
60
+ * *:csv_options* - Options for CSV generator. [Availible options](http://www.ruby-doc.org/stdlib-2.1.2/libdoc/csv/rdoc/CSV.html#method-c-new). Optional
61
+ * *:columns* - Array of variables and methods. Name of columns will be created based on your translations. Optional
62
+
63
+ Example
64
+
65
+ ```ruby
66
+ class ReportsController < ApplicationController
67
+ def index
68
+ @reports = Report.all
69
+
70
+ respond_to do |format|
71
+ format.csv { render csv: @reports, filename: 'custom-reports.csv', csv_options: { col_sep: '\t' }, columns: [:created_at, :title] }
72
+ end
73
+ end
74
+ end
75
+ ```
76
+
55
77
  ### Localize column's names
56
78
 
57
79
  If you have translations for model's attributes under scope [:activerecord, :attributes, \*model_name\*] names of columns will be
@@ -59,11 +81,11 @@ automatically translated.
59
81
 
60
82
  ### Customize CSV
61
83
 
62
- To customize your CSV file you should add next methods to model **csv_header**(class method), **csv_row**.
84
+ To customize your CSV file with different methods and name of columns you should add next methods to model **csv_header**(class method), **csv_row**.
63
85
 
64
- **csv_header** should return array with column's titles.
86
+ * **csv_header** should return array with column's titles.
65
87
 
66
- **csv_row** should return array with values for columns.
88
+ * **csv_row** should return array with values for columns.
67
89
 
68
90
  ```ruby
69
91
  class Cat < ActiveRecord::Base
@@ -7,7 +7,7 @@ module RailsCsvRenderer
7
7
  # Options is configuration set for generated CSV file
8
8
 
9
9
  def to_custom_csv(options = {})
10
- csv_options = default_csv_options.merge(options)
10
+ csv_options = default_csv_options.merge(options[:csv_options] || {})
11
11
 
12
12
  if is_active_record?
13
13
  if !(model.respond_to?(:csv_header) || model.respond_to?(:csv_row)) || model.class_variable_defined?(:@@dynamic_generated_csv_methods)
@@ -30,7 +30,7 @@ module RailsCsvRenderer
30
30
  private
31
31
 
32
32
  def define_csv_methods(options)
33
- columns = model.column_names
33
+ columns = options[:columns] || model.column_names
34
34
 
35
35
  model.class_variable_set(:@@dynamic_generated_csv_methods, true)
36
36
  model.class_eval(%Q{
@@ -3,12 +3,13 @@ require 'action_controller/metal/renderers'
3
3
 
4
4
  module RailsCsvRenderer
5
5
  class Renderer
6
- def initialize!
7
- ActionController::Renderers.add :csv do |obj, options|
6
+ def self.initialize!
7
+ ActionController::Renderers.add(:csv) do |obj, options|
8
8
  filename = options[:filename] || "#{ Rails.application.class.parent_name }-report-#{ Time.current }.csv"
9
- csv.extend RailsCsvRenderer::Renderable
10
- data = csv.to_custom_csv(options[:csv_options])
11
- send_data data, type: Mime::CSV, disposition: "attachment; filename=#{filename}"
9
+ obj.extend RailsCsvRenderer::Renderable
10
+ data = obj.to_custom_csv(options)
11
+ send_data(data, type: Mime::CSV, disposition: "attachment; filename=#{filename}")
12
+ end
12
13
  end
13
14
  end
14
15
  end
@@ -1,3 +1,3 @@
1
1
  module RailsCsvRenderer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -60,7 +60,7 @@ describe RailsCsvRenderer::Renderable do
60
60
 
61
61
  context 'with csv options' do
62
62
  it 'includes columns and methods with configured separators' do
63
- options = { col_sep: "\t", row_sep: "\r\n" }
63
+ options = { csv_options: { col_sep: "\t", row_sep: "\r\n" } }
64
64
  expect(renderable_collection.to_custom_csv(options)).to eql "Id\tFull name\r\n1\tJohn Smith\r\n2\tAdam Kowalczyk\r\n3\tDennis Menace\r\n"
65
65
  end
66
66
  end
@@ -73,6 +73,23 @@ describe RailsCsvRenderer::Renderable do
73
73
  owner.cats << Cat.first
74
74
  expect(renderable_collection.to_custom_csv).to eql "Id,Name,Person\n1,Kitty,#{ owner.id }\n"
75
75
  end
76
+
77
+ context 'options with :columns' do
78
+ it 'includes columns and methods in specific order' do
79
+ options = { columns: [:name] }
80
+ expect(renderable_collection.to_custom_csv(options)).to eql "Name\nKitty\n"
81
+ end
82
+
83
+ context 'with localized name of atributes' do
84
+ before { I18n.locale = :ru }
85
+ after { I18n.locale = :en }
86
+
87
+ it 'includes columns and methods in specific order with localized headerd' do
88
+ options = { columns: [:name] }
89
+ expect(renderable_collection.to_custom_csv(options)).to eql "Кличка\nKitty\n"
90
+ end
91
+ end
92
+ end
76
93
  end
77
94
  end
78
95
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_csv_renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Sheleh
@@ -61,6 +61,7 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
+ - CHANGELOG.md
64
65
  - Gemfile
65
66
  - LICENSE.txt
66
67
  - README.md