mr_common 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (24) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -2
  3. data/app/controllers/mr_common/registrations/export_controller.rb +4 -1
  4. data/app/models/concerns/mr_common/csv_exportable.rb +2 -11
  5. data/app/models/mr_common/csv_exporter.rb +35 -0
  6. data/app/models/mr_common/default_csv_export_adapter.rb +23 -0
  7. data/lib/mr_common/version.rb +1 -1
  8. data/spec/dummy/log/development.log +797 -0
  9. data/spec/dummy/log/test.log +704 -0
  10. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Ba/bA8hAXQKANJzBTuQbpIP80FrF8klqsuYOtwuosQ4c5M.cache +0 -0
  11. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/F2/F20HIwdY1_oIp6GrPRxocFAmMMtPNn05JWkUM0eiloY.cache +0 -0
  12. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/LT/LTto-AZOGT_7LqZRJJlYVLaZXme34JlW6i5YO1-hrGU.cache +1 -0
  13. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/M-/m-gYrUz1942LmvKayV1K_Do9TtnA6UvVY_7yHHSxBTY.cache +0 -0
  14. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/PE/pEFyHpYnSNZ7VFg7gEphAXtD136lqFr1bvNrjOVmGLw.cache +2 -0
  15. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/PF/pfcazMoEPzIlvi6vy1SWeojc3_EkULHyrhNJIbgLTXg.cache +0 -0
  16. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/cg/Cg9p5LkxKD-IRPNf-k1yZ_HWd6ZX3JzR0yWQ985G0ZU.cache +1 -0
  17. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/f4/F4dxUCWat3Nq8_1jItIFx4nkOhrAiNFEtQDOTQXbbQI.cache +1 -0
  18. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/iD/IdsFsTMyyW-5MqmBEcAYJWX01RqeThhknBeeEHP_x3Y.cache +0 -0
  19. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/iD/iddALNcjHFMWIbupLvJ9nEWVgsAlQRi6N-dimttO4lU.cache +1 -0
  20. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/iI/iIBc8Ww_W04keUlRNdzPYMs398pQYMiqA8A7fh5g2hQ.cache +1 -0
  21. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/n0/N0766whV3_I5PD4CDMXdk9UJd5IHi129OMooZK4kGtU.cache +0 -0
  22. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/vP/Vp6TmwPplvA0CbnC7GxMNgD6E2vUKJsrefNNq-2cX48.cache +1 -0
  23. data/spec/dummy/tmp/pids/server.pid +1 -1
  24. metadata +30 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb005c3dea79627be3524d98f143154af85c90fdeb4013dcce4f8e391b68ca39
4
- data.tar.gz: ee4ee1651df5e88554ca34c7e43c5426fff69d77fe32cc65f8c74607cd51ff0e
3
+ metadata.gz: 5e0885e32d5643b86e1da2fe11974a06eb9c5a327fd82bd7bebcf8d85484e8ef
4
+ data.tar.gz: 0e20a6e34659744854a5ed28eac632c92c4b5f7d969ad201d8fa31e31a21944a
5
5
  SHA512:
6
- metadata.gz: 12f522e9b24e11d31b97f8ada700461fd997e568ab84c4f8e9a9d9d89d7e2f5f74c5d302ac15d95b0ba6e3d089d87b26ff9d19c261b95acf2dad949e9524c9a6
7
- data.tar.gz: c2d7a1b9c9e8e8b7c8b5503ce16c0ce17dbc343ad6e7dcbaeb6cbe96079b46a8486d4e1aad581f74f72e96bff9ea3f73c813bfe6e87d346e8e8e73c5f7e68f97
6
+ metadata.gz: a1070f2831e2216c89b387e6a1fa4c0c0e88519f2d01a0fd93af120cefeeabeed64fc40fe572fbf809c67610f93f86dcbf8936f288b9ced626dc6be1a22cd48e
7
+ data.tar.gz: 3af9a459afe31200e94a344ac87299f9b09bc3c6a2d2e30156e7ae61a21bcb3dc0ded109747ac462db7a34c87c3120841bdac00219f340e946c10ac7dc4deac7
data/README.md CHANGED
@@ -10,6 +10,8 @@ Common utilities extracted from starter-reg-site for use elsewhere
10
10
  * MrCommon::Concerns::CSVExportable - include to add `.as_csv` to any ActiveRecord model
11
11
 
12
12
  ## Installation
13
+ Ensure devise is installed and a User class defines the instance method `mr_common_admin?`
14
+
13
15
  Add to Gemfile
14
16
 
15
17
  ```ruby
@@ -24,10 +26,16 @@ mount MrCommon::Engine, at: '/common'
24
26
  # If you plan to use the default registration model,
25
27
  # set the host app's root route to the default registration
26
28
  # form, and override the the templates. (See MrCommon::Reminder for more instructions )
27
- # root to: "mr_common/registrations/public#new"
29
+ root to: "mr_common/registrations/public#new"
30
+ ```
31
+
32
+ Generate view overrides in the host project
33
+
34
+ ```
35
+ $ bin/rails g mr_common:views
28
36
  ```
29
37
 
30
- Configure initializer
38
+ Configure an initializer if you need different values than the defaults listed here
31
39
  ```ruby
32
40
  # config/initializers/mr_common.rb
33
41
  MrCommon.base_controller_name = "::ApplicationController"
@@ -4,7 +4,10 @@ module MrCommon
4
4
  module Registrations
5
5
  class ExportController < BaseController
6
6
  def index
7
- send_data Registration.as_csv, filename: "Registrations-#{Date.today}.csv"
7
+ send_data(
8
+ Registration.as_csv(format_rules: Registration.try(:csv_format_rules)),
9
+ filename: "Registrations-#{Date.today}.csv"
10
+ )
8
11
  end
9
12
  end
10
13
  end
@@ -5,17 +5,8 @@ module MrCommon
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  class_methods do
8
- def as_csv
9
- CSV.generate do |csv|
10
- csv << csv_column_names
11
- all.each do |item|
12
- csv << item.attributes.values_at(*csv_column_names)
13
- end
14
- end
15
- end
16
-
17
- def csv_column_names
18
- column_names
8
+ def as_csv(adapter: nil, format_rules: nil)
9
+ CSVExporter.new(self, adapter: adapter, format_rules: format_rules).generate_csv
19
10
  end
20
11
  end
21
12
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MrCommon
4
+ class CSVExporter
5
+ attr_reader :exportable
6
+ attr_reader :adapter
7
+ attr_reader :format_rules
8
+
9
+ def initialize(exportable, adapter: nil, format_rules: nil)
10
+ @exportable = exportable
11
+ @adapter = adapter ? adapter.new(exportable) : DefaultCSVExportAdapter.new(exportable)
12
+ @format_rules = format_rules || {}
13
+ end
14
+
15
+ def generate_csv
16
+ CSV.generate do |csv|
17
+ csv << adapter.headers
18
+
19
+ adapter.collection.each do |item|
20
+ row = adapter.row_from_item(item)
21
+ csv << format_row(row)
22
+ end
23
+ end
24
+ end
25
+
26
+ def format_row(row)
27
+ format_rules.each do |column_name, proc|
28
+ idx = adapter.headers.index(column_name.to_s)
29
+ row[idx] = proc.call(row[idx])
30
+ end
31
+
32
+ row
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MrCommon
4
+ class DefaultCSVExportAdapter
5
+ attr_reader :exportable
6
+
7
+ def initialize(exportable)
8
+ @exportable = exportable
9
+ end
10
+
11
+ def collection
12
+ exportable.all
13
+ end
14
+
15
+ def row_from_item(item)
16
+ item.attributes.values_at(*headers)
17
+ end
18
+
19
+ def headers
20
+ exportable.column_names
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MrCommon
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  end