setting_accessors 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +10 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +131 -0
  5. data/.travis.yml +11 -7
  6. data/Appraisals +17 -0
  7. data/CHANGELOG.md +38 -1
  8. data/Gemfile +2 -0
  9. data/README.md +64 -124
  10. data/Rakefile +5 -27
  11. data/bin/console +15 -0
  12. data/bin/setup +10 -0
  13. data/gemfiles/rails_4.1.gemfile +7 -0
  14. data/gemfiles/rails_4.2.gemfile +7 -0
  15. data/gemfiles/rails_4.2.gemfile.lock +162 -0
  16. data/gemfiles/rails_5.0.gemfile +7 -0
  17. data/gemfiles/rails_5.0.gemfile.lock +169 -0
  18. data/gemfiles/rails_5.1.gemfile +7 -0
  19. data/gemfiles/rails_5.1.gemfile.lock +169 -0
  20. data/gemfiles/rails_5.2.gemfile +7 -0
  21. data/gemfiles/rails_5.2.gemfile.lock +177 -0
  22. data/lib/generators/setting_accessors/install_generator.rb +11 -9
  23. data/lib/generators/setting_accessors/templates/model.rb.erb +0 -24
  24. data/lib/setting_accessors.rb +14 -5
  25. data/lib/setting_accessors/accessor_generator.rb +66 -0
  26. data/lib/setting_accessors/converters/base.rb +24 -0
  27. data/lib/setting_accessors/converters/boolean_converter.rb +51 -0
  28. data/lib/setting_accessors/converters/integer_converter.rb +21 -0
  29. data/lib/setting_accessors/converters/polymorphic_converter.rb +11 -0
  30. data/lib/setting_accessors/converters/string_converter.rb +11 -0
  31. data/lib/setting_accessors/helpers.rb +28 -0
  32. data/lib/setting_accessors/integration.rb +83 -97
  33. data/lib/setting_accessors/internal.rb +37 -64
  34. data/lib/setting_accessors/setting_scaffold.rb +147 -214
  35. data/lib/setting_accessors/setting_set.rb +168 -0
  36. data/lib/setting_accessors/version.rb +3 -1
  37. data/lib/tasks/setting_accessors_tasks.rake +2 -0
  38. data/setting_accessors.gemspec +27 -19
  39. metadata +117 -143
  40. data/.codeclimate.yml +0 -66
  41. data/lib/setting_accessors/accessor.rb +0 -189
  42. data/lib/setting_accessors/converter.rb +0 -71
  43. data/lib/setting_accessors/integration_validator.rb +0 -15
  44. data/lib/setting_accessors/validator.rb +0 -144
  45. data/test/dummy/README.rdoc +0 -28
  46. data/test/dummy/Rakefile +0 -6
  47. data/test/dummy/app/assets/images/.keep +0 -0
  48. data/test/dummy/app/assets/javascripts/application.js +0 -13
  49. data/test/dummy/app/assets/stylesheets/application.css +0 -15
  50. data/test/dummy/app/controllers/application_controller.rb +0 -5
  51. data/test/dummy/app/controllers/concerns/.keep +0 -0
  52. data/test/dummy/app/helpers/application_helper.rb +0 -2
  53. data/test/dummy/app/mailers/.keep +0 -0
  54. data/test/dummy/app/models/.keep +0 -0
  55. data/test/dummy/app/models/concerns/.keep +0 -0
  56. data/test/dummy/app/models/post.rb +0 -2
  57. data/test/dummy/app/models/setting.rb +0 -59
  58. data/test/dummy/app/models/user.rb +0 -19
  59. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  60. data/test/dummy/bin/bundle +0 -3
  61. data/test/dummy/bin/rails +0 -4
  62. data/test/dummy/bin/rake +0 -4
  63. data/test/dummy/config.ru +0 -4
  64. data/test/dummy/config/application.rb +0 -25
  65. data/test/dummy/config/boot.rb +0 -5
  66. data/test/dummy/config/database.yml +0 -25
  67. data/test/dummy/config/environment.rb +0 -5
  68. data/test/dummy/config/environments/development.rb +0 -37
  69. data/test/dummy/config/environments/production.rb +0 -83
  70. data/test/dummy/config/environments/test.rb +0 -34
  71. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  72. data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  73. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  74. data/test/dummy/config/initializers/inflections.rb +0 -16
  75. data/test/dummy/config/initializers/mime_types.rb +0 -4
  76. data/test/dummy/config/initializers/session_store.rb +0 -3
  77. data/test/dummy/config/initializers/setting_accessors.rb +0 -1
  78. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  79. data/test/dummy/config/locales/en.yml +0 -23
  80. data/test/dummy/config/routes.rb +0 -56
  81. data/test/dummy/config/secrets.yml +0 -22
  82. data/test/dummy/config/settings.yml +0 -23
  83. data/test/dummy/db/migrate/20150102112106_create_users.rb +0 -9
  84. data/test/dummy/db/migrate/20150102115329_create_settings.rb +0 -12
  85. data/test/dummy/db/migrate/20150723114600_create_posts.rb +0 -11
  86. data/test/dummy/db/schema.rb +0 -40
  87. data/test/dummy/db/test.sqlite3 +0 -0
  88. data/test/dummy/lib/assets/.keep +0 -0
  89. data/test/dummy/public/404.html +0 -67
  90. data/test/dummy/public/422.html +0 -67
  91. data/test/dummy/public/500.html +0 -66
  92. data/test/dummy/public/favicon.ico +0 -0
  93. data/test/dummy/test/fixtures/posts.yml +0 -11
  94. data/test/dummy/test/models/post_test.rb +0 -19
  95. data/test/dummy/test/models/setting_test.rb +0 -143
  96. data/test/dummy/test/models/user_test.rb +0 -154
  97. data/test/generators/install_generator_test.rb +0 -15
  98. data/test/setting_accessors_test.rb +0 -4
  99. data/test/test_helper.rb +0 -31
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.2.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,177 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ setting_accessors (1.0.0)
5
+ activemodel (>= 4.2, <= 5.2)
6
+ activerecord (>= 4.2, <= 5.2)
7
+ activesupport (>= 4.2, <= 5.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (5.2.0)
13
+ actionpack (= 5.2.0)
14
+ nio4r (~> 2.0)
15
+ websocket-driver (>= 0.6.1)
16
+ actionmailer (5.2.0)
17
+ actionpack (= 5.2.0)
18
+ actionview (= 5.2.0)
19
+ activejob (= 5.2.0)
20
+ mail (~> 2.5, >= 2.5.4)
21
+ rails-dom-testing (~> 2.0)
22
+ actionpack (5.2.0)
23
+ actionview (= 5.2.0)
24
+ activesupport (= 5.2.0)
25
+ rack (~> 2.0)
26
+ rack-test (>= 0.6.3)
27
+ rails-dom-testing (~> 2.0)
28
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
+ actionview (5.2.0)
30
+ activesupport (= 5.2.0)
31
+ builder (~> 3.1)
32
+ erubi (~> 1.4)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
35
+ activejob (5.2.0)
36
+ activesupport (= 5.2.0)
37
+ globalid (>= 0.3.6)
38
+ activemodel (5.2.0)
39
+ activesupport (= 5.2.0)
40
+ activerecord (5.2.0)
41
+ activemodel (= 5.2.0)
42
+ activesupport (= 5.2.0)
43
+ arel (>= 9.0)
44
+ activestorage (5.2.0)
45
+ actionpack (= 5.2.0)
46
+ activerecord (= 5.2.0)
47
+ marcel (~> 0.3.1)
48
+ activesupport (5.2.0)
49
+ concurrent-ruby (~> 1.0, >= 1.0.2)
50
+ i18n (>= 0.7, < 2)
51
+ minitest (~> 5.1)
52
+ tzinfo (~> 1.1)
53
+ appraisal (2.2.0)
54
+ bundler
55
+ rake
56
+ thor (>= 0.14.0)
57
+ arel (9.0.0)
58
+ ast (2.4.0)
59
+ builder (3.2.3)
60
+ concurrent-ruby (1.1.3)
61
+ crass (1.0.4)
62
+ diff-lcs (1.3)
63
+ erubi (1.7.1)
64
+ generator_spec (0.9.4)
65
+ activesupport (>= 3.0.0)
66
+ railties (>= 3.0.0)
67
+ globalid (0.4.1)
68
+ activesupport (>= 4.2.0)
69
+ i18n (1.1.1)
70
+ concurrent-ruby (~> 1.0)
71
+ jaro_winkler (1.5.1)
72
+ loofah (2.2.3)
73
+ crass (~> 1.0.2)
74
+ nokogiri (>= 1.5.9)
75
+ mail (2.7.1)
76
+ mini_mime (>= 0.1.1)
77
+ marcel (0.3.3)
78
+ mimemagic (~> 0.3.2)
79
+ method_source (0.9.2)
80
+ mimemagic (0.3.2)
81
+ mini_mime (1.0.1)
82
+ mini_portile2 (2.3.0)
83
+ minitest (5.11.3)
84
+ nio4r (2.3.1)
85
+ nokogiri (1.8.5)
86
+ mini_portile2 (~> 2.3.0)
87
+ parallel (1.12.1)
88
+ parser (2.5.3.0)
89
+ ast (~> 2.4.0)
90
+ powerpack (0.1.2)
91
+ rack (2.0.6)
92
+ rack-test (1.1.0)
93
+ rack (>= 1.0, < 3)
94
+ rails (5.2.0)
95
+ actioncable (= 5.2.0)
96
+ actionmailer (= 5.2.0)
97
+ actionpack (= 5.2.0)
98
+ actionview (= 5.2.0)
99
+ activejob (= 5.2.0)
100
+ activemodel (= 5.2.0)
101
+ activerecord (= 5.2.0)
102
+ activestorage (= 5.2.0)
103
+ activesupport (= 5.2.0)
104
+ bundler (>= 1.3.0)
105
+ railties (= 5.2.0)
106
+ sprockets-rails (>= 2.0.0)
107
+ rails-dom-testing (2.0.3)
108
+ activesupport (>= 4.2.0)
109
+ nokogiri (>= 1.6)
110
+ rails-html-sanitizer (1.0.4)
111
+ loofah (~> 2.2, >= 2.2.2)
112
+ railties (5.2.0)
113
+ actionpack (= 5.2.0)
114
+ activesupport (= 5.2.0)
115
+ method_source
116
+ rake (>= 0.8.7)
117
+ thor (>= 0.18.1, < 2.0)
118
+ rainbow (3.0.0)
119
+ rake (10.5.0)
120
+ rspec (3.8.0)
121
+ rspec-core (~> 3.8.0)
122
+ rspec-expectations (~> 3.8.0)
123
+ rspec-mocks (~> 3.8.0)
124
+ rspec-core (3.8.0)
125
+ rspec-support (~> 3.8.0)
126
+ rspec-expectations (3.8.2)
127
+ diff-lcs (>= 1.2.0, < 2.0)
128
+ rspec-support (~> 3.8.0)
129
+ rspec-mocks (3.8.0)
130
+ diff-lcs (>= 1.2.0, < 2.0)
131
+ rspec-support (~> 3.8.0)
132
+ rspec-support (3.8.0)
133
+ rubocop (0.60.0)
134
+ jaro_winkler (~> 1.5.1)
135
+ parallel (~> 1.10)
136
+ parser (>= 2.5, != 2.5.1.1)
137
+ powerpack (~> 0.1)
138
+ rainbow (>= 2.2.2, < 4.0)
139
+ ruby-progressbar (~> 1.7)
140
+ unicode-display_width (~> 1.4.0)
141
+ ruby-progressbar (1.10.0)
142
+ sprockets (3.7.2)
143
+ concurrent-ruby (~> 1.0)
144
+ rack (> 1, < 3)
145
+ sprockets-rails (3.2.1)
146
+ actionpack (>= 4.0)
147
+ activesupport (>= 4.0)
148
+ sprockets (>= 3.0.0)
149
+ sqlite3 (1.3.13)
150
+ thor (0.20.3)
151
+ thread_safe (0.3.6)
152
+ tzinfo (1.2.5)
153
+ thread_safe (~> 0.1)
154
+ unicode-display_width (1.4.0)
155
+ websocket-driver (0.7.0)
156
+ websocket-extensions (>= 0.1.0)
157
+ websocket-extensions (0.1.3)
158
+ with_model (2.1.2)
159
+ activerecord (>= 4.2, < 6.0)
160
+
161
+ PLATFORMS
162
+ ruby
163
+
164
+ DEPENDENCIES
165
+ appraisal (~> 2.2)
166
+ bundler (~> 1.6)
167
+ generator_spec (~> 0.9)
168
+ rails (~> 5.2.0)
169
+ rake (~> 10.4)
170
+ rspec (~> 3.8)
171
+ rubocop (~> 0.60)
172
+ setting_accessors!
173
+ sqlite3 (~> 1.3)
174
+ with_model (~> 2.1)
175
+
176
+ BUNDLED WITH
177
+ 1.17.1
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SettingAccessors
2
4
  module Generators
3
5
  class InstallGenerator < Rails::Generators::Base
4
6
  include Rails::Generators::Migration
5
7
 
6
- source_root File.expand_path('../templates', __FILE__)
8
+ source_root File.expand_path('templates', __dir__)
7
9
 
8
- argument :model_name, :type => :string, :default => 'Setting'
10
+ argument :model_name, type: :string, default: 'Setting'
9
11
 
10
- def self.next_migration_number(path)
12
+ def self.next_migration_number(_nbr)
11
13
  if @prev_migration_nr
12
14
  @prev_migration_nr += 1
13
15
  else
@@ -22,12 +24,12 @@ module SettingAccessors
22
24
  template 'model.rb.erb', "app/models/#{model_name.classify.underscore}.rb"
23
25
  migration_template 'migration.rb.erb', "db/migrate/create_#{model_name.classify.underscore.pluralize}.rb"
24
26
 
25
- initializer 'setting_accessors.rb', <<INIT
26
- #The model your application is using for settings.
27
- #If you created it using the SettingAccessors generator, the
28
- #model name below should already be correct.
29
- SettingAccessors.setting_class = #{model_name}
30
- INIT
27
+ initializer 'setting_accessors.rb', <<~EOS
28
+ # The model your application is using for settings.
29
+ # If you created it using the SettingAccessors generator, the
30
+ # model name below should already be correct.
31
+ SettingAccessors.setting_class = #{model_name}
32
+ EOS
31
33
  end
32
34
  end
33
35
  end
@@ -20,28 +20,4 @@ class <%= model_name.camelize %> < ActiveRecord::Base
20
20
  belongs_to :assignable, :polymorphic => true
21
21
 
22
22
  include SettingAccessors::SettingScaffold
23
-
24
- #
25
- # Makes accessing settings a little easier.
26
- # Examples:
27
- #
28
- # #Loading **the value** of a global setting named "my_setting"
29
- # Setting.my_setting
30
- #
31
- # #Setting **the value** of a global setting named "my_setting"
32
- # Setting.my_setting = [1,2,3,4,5]
33
- #
34
- # #Loading **the value** of an assigned setting named "cool_setting"
35
- # #+some_cool_user+ is here an instance of ActiveRecord::Base
36
- # Setting.cool_setting(some_cool_user)
37
- #
38
- def self.method_missing(method, *args)
39
- super(method, *args) if args.size > 1
40
- method_name = method.to_s
41
- if method_name.last == '='
42
- self.create_or_update(method_name[0..method_name.length-2], args.first, nil, true)
43
- else
44
- self.get(method_name, args.first)
45
- end
46
- end
47
23
  end
@@ -1,11 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_model/validations'
5
+
1
6
  require 'setting_accessors/version'
2
- require 'setting_accessors/accessor'
3
- require 'setting_accessors/converter'
7
+ require 'setting_accessors/helpers'
8
+ require 'setting_accessors/accessor_generator'
9
+ require 'setting_accessors/converters/base'
10
+ require 'setting_accessors/converters/boolean_converter'
11
+ require 'setting_accessors/converters/integer_converter'
12
+ require 'setting_accessors/converters/string_converter'
13
+ require 'setting_accessors/converters/polymorphic_converter'
4
14
  require 'setting_accessors/integration'
5
- require 'setting_accessors/integration_validator'
6
15
  require 'setting_accessors/internal'
7
16
  require 'setting_accessors/setting_scaffold'
8
- require 'setting_accessors/validator'
17
+ require 'setting_accessors/setting_set'
9
18
 
10
19
  ActiveRecord::Base.class_eval do
11
20
  include SettingAccessors::Integration
@@ -13,7 +22,7 @@ end
13
22
 
14
23
  module SettingAccessors
15
24
  def self.setting_class
16
- self.setting_class_name.constantize
25
+ setting_class_name.constantize
17
26
  end
18
27
 
19
28
  def self.setting_class=(klass)
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # This class handles creating everything necessary for a new setting_accessor
5
+ # and the actual method assignment in the calling class.
6
+ #
7
+ module SettingAccessors
8
+ class AccessorGenerator
9
+ attr_reader :setting_name
10
+ attr_reader :options
11
+
12
+ def initialize(setting_name, **options)
13
+ @setting_name = setting_name
14
+ @options = options
15
+ end
16
+
17
+ def assign_setting!(klass)
18
+ SettingAccessors::Internal.set_class_setting(klass, setting_name, options)
19
+ define_getters(klass)
20
+ define_setter(klass)
21
+ define_active_record_helpers(klass)
22
+ end
23
+
24
+ private
25
+
26
+ def define_getters(klass, outer_setting_name = setting_name)
27
+ setting_type = SettingAccessors::Internal.setting_value_type(setting_name, klass).to_sym
28
+
29
+ klass.class_eval do
30
+ define_method(outer_setting_name) do
31
+ settings.get_or_default(outer_setting_name)
32
+ end
33
+
34
+ # Getter alias for boolean settings
35
+ alias_method "#{outer_setting_name}?", outer_setting_name if setting_type == :boolean
36
+ end
37
+ end
38
+
39
+ def define_setter(klass, outer_setting_name = setting_name)
40
+ klass.class_eval do
41
+ define_method("#{outer_setting_name}=") do |new_value|
42
+ settings[outer_setting_name] = new_value
43
+ end
44
+ end
45
+ end
46
+
47
+ def define_active_record_helpers(klass, outer_setting_name = setting_name)
48
+ klass.class_eval do
49
+ # NAME_was
50
+ define_method("#{outer_setting_name}_was") do
51
+ settings.value_was(outer_setting_name)
52
+ end
53
+
54
+ # NAME_before_type_cast
55
+ define_method("#{outer_setting_name}_before_type_cast") do
56
+ settings.value_before_type_cast(outer_setting_name)
57
+ end
58
+
59
+ # NAME_changed?
60
+ define_method("#{outer_setting_name}_changed?") do
61
+ settings.value_changed?(outer_setting_name)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SettingAccessors
4
+ module Converters
5
+ class Base
6
+ attr_reader :value
7
+
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+
12
+ def convert
13
+ # ActiveRecord does not convert +nil+ values to the corresponding database type
14
+ return value if value.nil?
15
+
16
+ parse_value
17
+ end
18
+
19
+ def self.parse_value
20
+ raise NotImplementedError
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SettingAccessors
4
+ module Converters
5
+ class BooleanConverter < Base
6
+ def parse_value
7
+ case value
8
+ when TrueClass, FalseClass
9
+ value
10
+ when String
11
+ parse_string
12
+ when Integer
13
+ parse_integer
14
+ else
15
+ parse_other_value
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ #
22
+ # Special handler for Rails 4.2, see the following deprecation warning:
23
+ #
24
+ # DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` (0.0)
25
+ # to a boolean column. Currently this value casts to `false`.
26
+ # This will change to match Ruby's semantics, and will cast to `true` in Rails 5.
27
+ # If you would like to maintain the current behavior, you should explicitly handle
28
+ # the values you would like cast to `false`.
29
+ #
30
+ def parse_other_value
31
+ Gem.loaded_specs['activerecord'].version >= Gem::Version.create('5.0')
32
+ end
33
+
34
+ def parse_integer
35
+ return true if value.to_i == 1
36
+ return false if value.to_i.zero?
37
+
38
+ nil
39
+ end
40
+
41
+ def parse_string
42
+ case value.downcase
43
+ when 'true', '1' then true
44
+ when 'false', '0' then false
45
+ when '' then nil
46
+ else parse_other_value
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SettingAccessors
4
+ module Converters
5
+ class IntegerConverter < Base
6
+ def parse_value
7
+ return parse_boolean if value == true || value == false
8
+
9
+ value.to_i
10
+ rescue NoMethodError
11
+ nil
12
+ end
13
+
14
+ private
15
+
16
+ def parse_boolean
17
+ value ? 1 : 0
18
+ end
19
+ end
20
+ end
21
+ end