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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6ba51cce908ff75943938e86e01d1aed48b0ccf6
4
- data.tar.gz: 7d164c7545cb6adfa0741b8dabf3b4a7bfae5a74
2
+ SHA256:
3
+ metadata.gz: ccf27bd36edbc82a68d4f3030c58e3582f71c63dafd2289c5fb483bfd8a81263
4
+ data.tar.gz: 153d794f0323bb0f8518a8331145766df1ca8fc935c88a4419f9b5772c2226db
5
5
  SHA512:
6
- metadata.gz: ee4917d83045b732589124c938999204b8458859707119faa78d7c2aa3626d4f0949d1cf34afc4d994dda2f4740ceefe4679ff2aec004e7f0382bc2a1c6f1cf7
7
- data.tar.gz: 3feb815f194119f2817314c3b6b01880083132eae3972dff75b9ae38c821ae1ee9a4bf105d5105294af7b84ad6c901508a0804f132e5562b0e7a46779bd0ac1f
6
+ metadata.gz: ff2f5b55fb799e97f14272962f832de8121ee549aa53a7c988a205e7028b4d9b9ad1f5cd5f141a5ce8c8d5d29b494b7ffff2e1ead3929dae4e5df3406f64d285
7
+ data.tar.gz: 1c7819afb12ef5cfd4de81ca05e6c3c5605eb1c9e2173a977bcd806b64b229443733e13b1c31b18dc3579a1fc17ca1b4677ee6f7d7c47bc701caa2a61e1b32e0
data/.editorconfig ADDED
@@ -0,0 +1,10 @@
1
+ [*]
2
+ charset = utf-8
3
+ end_of_line = lf
4
+ indent_size = 2
5
+ indent_style = space
6
+ insert_final_newline = true
7
+ trim_trailing_whitespace = true
8
+
9
+ [*.md]
10
+ trim_trailing_whitespace = false
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,131 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Exclude:
4
+ - tmp/**/*
5
+ - gemfiles/**/*
6
+
7
+ #---------------------------------------------
8
+ # Layout
9
+ #---------------------------------------------
10
+
11
+ # Hashes do not need padding
12
+ Layout/SpaceInsideHashLiteralBraces:
13
+ Enabled: false
14
+
15
+ # Allow 2 space indentation for when inside a case
16
+ Layout/CaseIndentation:
17
+ Enabled: false
18
+
19
+ # Allow empty lines in classes
20
+ Layout/EmptyLinesAroundClassBody:
21
+ Enabled: false
22
+
23
+ # Allow multiple spaces before first argument
24
+ Layout/SpaceBeforeFirstArg:
25
+ Enabled: false
26
+
27
+ # Allow extra spacing, e.g. to align components
28
+ Layout/ExtraSpacing:
29
+ Enabled: false
30
+
31
+ # Usually good, but in some cases not possible
32
+ Layout/AlignHash:
33
+ Enabled: false
34
+
35
+ # Allow an empty line after do / before end
36
+ Layout/EmptyLinesAroundBlockBody:
37
+ Enabled: false
38
+
39
+ # Again, generally a good idea, but it has problems with multiline operations in
40
+ # combination with assignments
41
+ Layout/MultilineOperationIndentation:
42
+ Enabled: false
43
+
44
+ # See the corresponding other cops
45
+ Layout/EmptyLinesAroundModuleBody:
46
+ Enabled: false
47
+
48
+ Layout/SpaceInLambdaLiteral:
49
+ Enabled: false
50
+
51
+ #---------------------------------------------
52
+ # Metrics
53
+ #---------------------------------------------
54
+
55
+ # Allow bigger classes
56
+ Metrics/ClassLength:
57
+ Enabled: false
58
+
59
+ Metrics/LineLength:
60
+ Max: 120
61
+
62
+ # To make it possible to copy or click on URIs in the code, we allow lines
63
+ # containing a URI to be longer than Max.
64
+ AllowHeredoc: true
65
+ AllowURI: true
66
+
67
+ Metrics/BlockLength:
68
+ Max: 75
69
+ Exclude:
70
+ - spec/**/*.rb
71
+
72
+ # Allow longer methods
73
+ Metrics/MethodLength:
74
+ Enabled: false
75
+
76
+ # Allow bigger modules
77
+ Metrics/ModuleLength:
78
+ Enabled: false
79
+
80
+ #---------------------------------------------
81
+ # Naming
82
+ #---------------------------------------------
83
+
84
+ Naming/HeredocDelimiterNaming:
85
+ Enabled: false
86
+
87
+ #---------------------------------------------
88
+ # Style
89
+ #---------------------------------------------
90
+
91
+ # Allow fail() for initial exception, raise() for re-raise
92
+ # It seems that the cop decision was mainly based on "more people use raise than fail"...
93
+ Style/SignalException:
94
+ Enabled: false
95
+
96
+ # Allow assigning multiple variables in one line.
97
+ # This should not be overused, but comes in handy when assigning initializer values to instance variables
98
+ Style/ParallelAssignment:
99
+ Enabled: false
100
+
101
+ # Depending on the situation, it might make more sense to use
102
+ # [:symbol1, :symbol2] over %i[symbol1 symbol2], e.g. for multiline aligning reasons.
103
+ Style/SymbolArray:
104
+ Enabled: false
105
+
106
+ # Not all modules have to have top level comments
107
+ Style/Documentation:
108
+ Enabled: false
109
+
110
+ # Allow class variable usage
111
+ Style/ClassVars:
112
+ Enabled: false
113
+
114
+ # Allow block comments
115
+ Style/BlockComments:
116
+ Enabled: false
117
+
118
+ # Allow the use of !! (conversion of nil/object to true/false)
119
+ Style/DoubleNegation:
120
+ Enabled: false
121
+
122
+ # Allow unless/if blocks even for one-liners
123
+ Style/IfUnlessModifier:
124
+ Enabled: false
125
+
126
+ Style/GuardClause:
127
+ Enabled: false
128
+
129
+ Style/MissingRespondToMissing:
130
+ Exclude:
131
+ - lib/setting_accessors/setting_scaffold.rb
data/.travis.yml CHANGED
@@ -2,19 +2,23 @@ language: ruby
2
2
  cache: bundler
3
3
 
4
4
  rvm:
5
- - 2.2
6
- - 2.3.1
5
+ - 2.3.0
6
+ - 2.3.3
7
+ - 2.4.0
8
+ - 2.4.4
9
+ - 2.5.0
10
+ - 2.5.3
7
11
 
8
- env:
9
- - DISABLE_DATABASE_ENVIRONMENT_CHECK=1
12
+ gemfile:
13
+ - gemfiles/rails_4.2.gemfile
14
+ - gemfiles/rails_5.0.gemfile
15
+ - gemfiles/rails_5.1.gemfile
16
+ - gemfiles/rails_5.2.gemfile
10
17
 
11
18
  before_install: 'gem install bundler'
12
19
  script: 'bundle exec rake'
13
20
 
14
21
  notifications:
15
22
  email:
16
- recipients:
17
- - stex@sterex.de
18
23
  on_failure: change
19
24
  on_success: never
20
-
data/Appraisals ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'rails-4.2' do
4
+ gem 'rails', '~> 4.2.0'
5
+ end
6
+
7
+ appraise 'rails-5.0' do
8
+ gem 'rails', '~> 5.0.0'
9
+ end
10
+
11
+ appraise 'rails-5.1' do
12
+ gem 'rails', '~> 5.1.0'
13
+ end
14
+
15
+ appraise 'rails-5.2' do
16
+ gem 'rails', '~> 5.2.0'
17
+ end
data/CHANGELOG.md CHANGED
@@ -1,5 +1,42 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.0
4
+
5
+ This is almost a complete refactoring of the gem which also removes some not really needed functionality.
6
+ It is the first release fully compatible with Rails 5.
7
+
8
+ New minimum versions:
9
+
10
+ * Ruby: 2.3
11
+ * Rails: 4.2
12
+
13
+ #### General
14
+
15
+ * Removed globally defined settings (`config/settings.yml`) and therefore also global validations and type conversions.
16
+ It is still possible to create global Settings, but each developer has to make sure that the assigned
17
+ value is valid and correctly typed.
18
+ * Type conversion follows AR's type conversions more strictly now.
19
+ This means that e.g. assigning `1.0` to a boolean setting will result in `true` instead of `nil` as before.
20
+ * Updating an attribute created through `setting_accessor` now leads to the record being touched,
21
+ similar to what would happen when a normal database attribute was changed. (#4)
22
+ * Setting a new value for an attribute created through `attribute_accessor` will now mark
23
+ the record as "dirty", meaning `changed?` returns true. (#9)
24
+ This was especially needed for Rails 5 as it only even performs a database operation in this case.
25
+
26
+ #### Tests
27
+
28
+ * Tests are now written in RSpec
29
+ * The dummy Rails application has been removed and replaced with `with_model`
30
+ * Appraisal was added to test against multiple Rails versions
31
+
32
+ #### API Changes
33
+
34
+ * `method_missing` is now part of the SettingScaffold and therefore no longer needed in the actual Setting model.
35
+ You should remove `method_missing` from your model.
36
+ * Setting.create_or_update was renamed to Setting.set and now uses a keyword argument for `assignable`
37
+ * `setting_accessor` no longer supports the `fallback` option.
38
+ If a `default` option is given, it is automatically used as fallback.
39
+
3
40
  ## 0.3.0
4
41
 
5
- * Fixed a bug that caused setting accessors not being initialized when being called as part of a rake task chain (#6)
42
+ * Fixed a bug that caused setting accessors not being initialized when being called as part of a rake task chain (#6)
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in setting_accessors.gemspec
data/README.md CHANGED
@@ -3,6 +3,7 @@ SettingAccessors
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/setting_accessors.svg)](http://badge.fury.io/rb/setting_accessors)
5
5
  [![Build Status](https://travis-ci.org/Stex/setting_accessors.svg?branch=master)](https://travis-ci.org/Stex/setting_accessors)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/78becd1d005aab2d1409/maintainability)](https://codeclimate.com/github/Stex/setting_accessors/maintainability)
6
7
 
7
8
  Sometimes it's handy to keep track of various settings or attributes in ActiveRecord instances and persist them through various requests (and sessions). An example would be to store a items-per-page value for multiple tables in the application per user or probably also set default sorting directions.
8
9
 
@@ -11,6 +12,20 @@ The only problem is, that it would be necessary to either keep hundreds of colum
11
12
  This gem consists of a global key-value-store, allowing to use both global and record bound settings
12
13
  and ActiveRecord integration to add virtual columns to models without having to change the database layout.
13
14
 
15
+ Version Information / Backwards Compatibility
16
+ ---------------------------------------------
17
+
18
+ The current version (1.x) is only compatible with Ruby >= 2.3 and Rails >= 4.2.
19
+ Versions 0.x support older versions of both Ruby and Rails.
20
+
21
+ Version 0.x also supported validations and type conversions for globally defined settings
22
+ (`Setting.my_setting = 5`).
23
+ This was removed in this version, so developers should make sure that the assigned
24
+ value has the correct type themselves.
25
+
26
+ Validations for class-wise defined `setting_accessor`s can still be realized using
27
+ ActiveModel's integrated validators as described below.
28
+
14
29
  Installation
15
30
  ------------
16
31
 
@@ -37,7 +52,6 @@ The gem requires a few additional files to work properly:
37
52
  - A migration to create the settings table
38
53
  - A setting model
39
54
  - An initializer
40
- - A settings config file (config/settings.yml)
41
55
 
42
56
  All of them can be generated using the provided generator:
43
57
 
@@ -47,12 +61,15 @@ $ rails g setting_accessors:install MODEL_NAME
47
61
 
48
62
  If no model name is given, the default one (`Setting`) is used.
49
63
 
50
- Usage as a global key-value-store (globally defined and anonymous settings)
64
+ Usage as a global key-value-store
51
65
  -----
52
66
 
53
- In the following, the model name will be assumed as `Setting`, though you may choose its name freely using the provided generator (see above).
67
+ In the following, the model name will be assumed as `Setting`,
68
+ though you may choose its name freely using the provided generator (see above).
54
69
 
55
- Settings can either be global or assigned to an instance of ActiveRecord::Base. They consist of a name which is unique either in the global scope or within the instance and a value which can be everything that's serializable through YAML.
70
+ Settings can either be global or assigned to an instance of ActiveRecord::Base.
71
+ They consist of a name which is unique either in the global scope or within the instance
72
+ and a value which can be everything that's serializable through YAML.
56
73
 
57
74
  The easiest way to use the settings model is as a global key-value-store without validations and typecasting.
58
75
 
@@ -64,7 +81,9 @@ Setting.the_meaning_of_life
64
81
  #=> 42
65
82
  ```
66
83
 
67
- If the name contains characters which are not allowed due to ruby naming rules, you can also use the methods`Setting[KEY]` resp. `Setting.get(KEY)` to get a setting's value and `Setting[KEY] = VALUE` resp.`Setting.create_or_update(KEY, VALUE)`.
84
+ If the name contains characters which are not allowed due to ruby naming rules,
85
+ you can also use the methods`Setting[KEY]` resp. `Setting.get(KEY)` to get a setting's value
86
+ and `Setting[KEY] = VALUE` resp.`Setting.set(KEY, VALUE)`.
68
87
 
69
88
  Therefore, the following getter methods are equivalent:
70
89
 
@@ -79,54 +98,23 @@ For the corresponding setters:
79
98
  ```ruby
80
99
  Setting.meaning_of_life = 42
81
100
  Setting[:meaning_of_life] = 42
82
- Setting.create_or_update(:meaning_of_life, 42)
101
+ Setting.set(:meaning_of_life, 42)
83
102
  ```
84
103
 
85
- ### Globally defined settings with types and validations
86
-
87
- As stated above, the initializer will generate a file called `settings.yml` in your application's `config` directory. This file is used to define global settings, an entry consists of:
88
-
89
- - The setting's name
90
- - The setting's type (optional)
91
- - Validations to be performed when the setting is saved (optional)
92
- - A default value (optional)
93
-
94
- An example would be a simple string setting:
95
-
96
- ```yaml
97
- a_string:
98
- type: string
99
- default: "I am a string!"
100
- validations:
101
- presence: true
102
- ```
103
-
104
- If a setting is defined with a type, automatic type conversion will happen, similar to what ActiveRecord does:
104
+ This makes it also possible to use Ruby's or-equals operator:
105
105
 
106
106
  ```ruby
107
- Setting.a_string = 42
108
- #=> "42"
109
- ```
110
-
111
- The default value is used by the functions `Setting.get_or_default` and `Setting.create_default_setting` and a fallback option for assigned settings (see below).
112
-
113
- The built-in validations currently support, this might be extended in the future.
114
-
115
- | Base Validation | Options |
116
- |:----------------|:---------------------------|
117
- | `presence` | `allow_nil`, `allow_blank` |
118
- | `numericality` | `only_integer` |
119
- | `boolean` |   |
120
-
107
+ Setting.meaning_of_life ||= 42
108
+ Setting[:meaning_of_life] ||= 42
109
+ ```
121
110
 
122
111
  ### Assigned Records
123
112
 
124
- Both globally defined settings and "anonymous" settings can also be assigned to
125
- a instances of `ActiveRecord::Base` without having to define them in the model first.
113
+ Global settings can also be assigned to an instance of `ActiveRecord::Base` without
114
+ having to define them in the model first.
126
115
 
127
- An example would be the above mentioned saving of "items per page" values for
128
- various views: Let's say we have an events view and want to save how many
129
- rows/items each user would like to display.
116
+ An example would be to save "items per page" values for various views:
117
+ Let's say we have an events view and want to save how many rows/items each user would like to display.
130
118
 
131
119
  As there might be many views requiring this functionality, it wouldn't make
132
120
  sense to define global settings for each of them. Instead, we would use anonymous
@@ -138,122 +126,74 @@ def items_per_page
138
126
  default_value = 30
139
127
  Setting.get(key, current_user) || default_value
140
128
  end
141
- ```
142
-
143
- ActiveRecord Integration
144
- ------------------------
145
-
146
- The gem adds the method `setting_accessor` to each class which inherits from `ActiveRecord::Base`.
147
-
148
- `setting_accessor` takes a setting name and a set of options to customize the accessor's behaviour:
149
129
 
150
- ```ruby
151
- class MyModel < ActiveRecord::Base
152
- setting_accessor :a_string, :fallback => :default
130
+ def set_items_per_page
131
+ key = [controller_path, action_name].join('_')
132
+ Setting.set(key, params[:per_page], assignable: current_user) || default_value
153
133
  end
154
134
  ```
155
135
 
156
- This automatically adds most of the helper methods to the model instances which are available for database columns:
136
+ The following calls are equivalent:
157
137
 
158
138
  ```ruby
159
- my_model = MyModel.new
160
-
161
- #Setter
162
- my_model.a_string = 1234
163
-
164
- #Getter
165
- my_model.a_string
166
- #=> "1234"
167
-
168
- #Value before type cast
169
- my_model.a_string_before_type_cast
170
- #=> 1234
171
-
172
- #Old value
173
- my_model.a_string_was
174
- #=> "I am a string!" (fallback value, see below)
175
-
176
- #Check if the value was changed
177
- my_model.a_string_changed?
178
- #=> true
139
+ Setting[:meaning_of_life, current_universe] = 42
140
+ Setting.set(:meaning_of_life, 42, assignable: current_universe)
179
141
  ```
180
142
 
181
- The setting records are only persisted if all settings *and* the main record were valid.
182
-
183
- ### Integration of globally defined settings
143
+ `[]=` is not an exact alias of `set` here to still support `or-else`:
184
144
 
185
- If a setting with the given name is defined in `config/settings.yml`, validations and type settings are automatically fetched from it, in this case, only the `:fallback` option is allowed.
145
+ ```ruby
146
+ Setting[:meaning_of_life, current_universe] ||= 42
147
+ ```
186
148
 
187
- As of now, this means that custom validations are also not supported for globally defined settings, this may be changed in the future.
149
+ ActiveRecord Integration
150
+ ------------------------
188
151
 
189
- ### Class-wise definition of settings
152
+ The gem adds the method `setting_accessor` to each class which inherits from `ActiveRecord::Base`.
190
153
 
191
- If a setting is defined using `setting_accessor` which is not part of `config/settings.yml`, it will only be available in this class. It however may be defined in multiple classes independently.
154
+ It basically generates an `attribute_accessor` with the given name, but also makes sure that
155
+ the value is persisted when the actual record is saved. It also provides the standard attribute helper
156
+ methods ActiveRecord adds for each database column (`ATTRIBUTE_changed?`, `ATTRIBUTE_was`, ...).
192
157
 
193
- Defining a class setting accepts the same options as the config file:
158
+ It aims to simulate an actual database column.
194
159
 
195
160
  ```ruby
196
161
  class MyModel < ActiveRecord::Base
197
- setting_accessor :my_setting, :type => :boolean, :default => false
162
+ setting_accessor :my_setting, type: :boolean, default: false
198
163
  end
199
164
  ```
200
165
 
201
166
  ### Options
202
167
 
203
168
  ```ruby
204
- :type => :string | :integer | :boolean | :polymorphic
169
+ type: :string | :integer | :boolean | :polymorphic
205
170
  ```
206
171
 
207
- `:type` defines the setting's data type. If no type is given, `:polymorhpic`
208
- is used automatically.
172
+ `:type` defines the setting's data type. If no type is given, `:polymorhpic` is used automatically.
209
173
 
210
174
  For every other type, the setting's value is automatically converted accordingly
211
- upon setting it, mimicking ActiveRecord's behaviour regarding database columns.
175
+ upon setting it, mimicking ActiveRecord's type casting based on database types.
212
176
  Please note that `:polymorphic` values are saved as is, meaning that you can
213
177
  store everything that's serializable.
214
178
 
215
- More types will be added in the future, most likely all database types
216
- ActiveRecord can handle as well.
217
-
218
179
  ```ruby
219
- :default => "I am a string!"
180
+ default: Object
220
181
  ```
221
182
 
222
- `:default` sets the setting's default value. It can be retrieved either by
223
- calling `Setting.get_or_default(...)` or defining a `:fallback` on a class setting.
224
-
225
- ```ruby
226
- :fallback => :global | :default | Object
227
- ```
183
+ `:default` sets the setting's default value that is returned as long no actual setting exists for
184
+ that name/assignable.
228
185
 
229
- The `:fallback` option specifies which value should be returned in case the
230
- setting did not receive a value yet:
231
-
232
- - `:global` will try to retrieve a global setting (`Setting.NAME`) with the same
233
- name as the accessor and return `nil` if it isn't defined.
234
- - `:default` will return the default value set up either in the accessor method
235
- or `config/settings.yml` for globally defined settings.
236
- - Every other value will be returned as is
237
-
238
-
239
- ```ruby
240
- :validations => {:presence => true}
241
- :validations => {:numericality => {:only_integer => true}}
242
- :validations => {:custom => [lambda {|setting| setting.errors.add(:not_42) if setting.value != 42}]}
243
- :validations => {:custom => [:must_be_42]}
244
- ```
186
+ ### Validations
245
187
 
246
- There are some built-in validations (see above), but they should be mostly used for globally defined
247
- and globally used settings (`Setting.somethingsomething`).
248
- For model specific settings, you may treat the setting accessors just like normal columns
249
- and define your validations accordingly, e.g.
188
+ Attributes created by `setting_accessor` can simply be used with `ActiveModel::Validations` the same way
189
+ as other attributes:
250
190
 
251
191
  ```ruby
252
- setting_accessor :my_string, :type => :string
253
- setting_accessor :my_number, :type => :integer
192
+ setting_accessor :my_string, type: :string
193
+ setting_accessor :my_number, type: :integer
254
194
 
255
- validates :my_string, :presence => true
256
- validates :my_number, :numericality => {:only_integer => true}
195
+ validates :my_string, presence: true
196
+ validates :my_number, numericality: {only_integer: true}
257
197
  ```
258
198
 
259
199
  Contributing