setting_accessors 0.3.0 → 1.0.0

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.
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
File without changes
@@ -1,11 +0,0 @@
1
- # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
-
3
- # This model initially had no columns defined. If you add columns to the
4
- # model remove the '{}' from the fixture names and add the columns immediately
5
- # below each fixture, per the syntax in the comments below
6
- #
7
- one: {}
8
- # column: value
9
- #
10
- two: {}
11
- # column: value
@@ -1,19 +0,0 @@
1
- require_relative '../../../test_helper'
2
-
3
- class PostTest < ActiveSupport::TestCase
4
- context 'JSON serialization' do
5
- setup do
6
- @post = Post.new(:title => 'a post', :text => 'a content')
7
- end
8
-
9
- should 'work without having any setting_accessors defined' do
10
- assert @post.as_json
11
- end
12
-
13
- should 'contain all original public attributes' do
14
- [:title, :text, :id, :created_at, :updated_at].each do |attr|
15
- assert_includes @post.as_json.keys, attr.to_s
16
- end
17
- end
18
- end
19
- end
@@ -1,143 +0,0 @@
1
- require_relative '../../../test_helper'
2
-
3
- class SettingTest < ActiveSupport::TestCase
4
- should validate_presence_of :name
5
-
6
- #FIXME: This is currently setting assignable_type and assignable_id to "0" resp. "1" which breaks everything.
7
- # should validate_uniqueness_of(:name).scoped_to([:assignable_type, :assignable_id])
8
-
9
- context 'Global Setting Accessors (method missing)' do
10
-
11
- should 'ignore a setting function if more than 1 argument is given' do
12
- assert_raises(NoMethodError) { Setting.my_setting(1, 2, 3) }
13
- end
14
-
15
- should 'return nil for a non-existing setting' do
16
- assert_nil Setting.gotta_catch_em_all
17
- end
18
-
19
- should 'create a new setting if necessary' do
20
- assert Setting.count.zero?
21
- assert Setting.gotta_catch_em_all = 'Pokemon!'
22
- assert Setting.count == 1
23
- end
24
-
25
- should "return the setting's value for an existing setting" do
26
- assert Setting.gotta_catch_em_all = 'Pokemon!'
27
- assert_equal Setting.gotta_catch_em_all, 'Pokemon!'
28
- end
29
-
30
- should 'update an existing setting instead of creating a new one' do
31
- assert Setting.count.zero?
32
- assert Setting.gotta_catch_em_all = 'Pokemon!'
33
- assert Setting.gotta_catch_em_all = 'Pokemon!'
34
- assert Setting.count == 1
35
- end
36
-
37
- should 'return assignable specific settings if an assignable is given' do
38
- ash = User.create(:first_name => 'Ash', :last_name => 'Ketchum')
39
- gary = User.create(:first_name => 'Gary', :last_name => 'Oak')
40
- team_rocket = User.create(:first_name => 'Jessie', :last_name => 'James')
41
-
42
- assert Setting.create_or_update(:pokedex_count, 151, ash)
43
- assert Setting.create_or_update(:pokedex_count, 1, gary)
44
-
45
- assert_nil Setting.pokedex_count
46
-
47
- assert_equal Setting.pokedex_count(ash), 151
48
- assert_equal Setting.pokedex_count(gary), 1
49
-
50
- #They don't want to be on file.
51
- assert_nil Setting.pokedex_count(team_rocket)
52
- end
53
- end
54
-
55
- context 'The create_or_update function' do
56
- setup do
57
- @ash = User.create(:first_name => 'Ash', :last_name => 'Ketchum')
58
- end
59
-
60
- should 'create a new assigned setting if it did not exist before' do
61
- assert Setting.count.zero?
62
- Setting.create_or_update(:pokedex_count, 151, @ash)
63
- assert Setting.count == 1
64
- end
65
-
66
- should 'update an assigned setting if it already exists' do
67
- Setting.create_or_update(:pokedex_count, 150, @ash)
68
- assert Setting.count == 1
69
- Setting.create_or_update(:pokedex_count, 151, @ash)
70
- assert Setting.count == 1
71
- assert_equal Setting.pokedex_count(@ash), 151
72
- end
73
-
74
- should 'return a setting object by default' do
75
- assert_instance_of Setting, Setting.create_or_update(:pokedex_count, 151, @ash)
76
- end
77
-
78
- should 'return just the value if wished' do
79
- assert_equal Setting.create_or_update(:pokedex_count, 151, @ash, true), 151
80
- end
81
- end
82
-
83
- context 'The globally_defined method' do
84
- should 'return true for a setting which is defined in config/settings.yml' do
85
- assert SettingAccessors::Internal.globally_defined_setting?('a_string')
86
- end
87
-
88
- should 'return false for a setting which is not defined in config/settings.yml' do
89
- assert !SettingAccessors::Internal.globally_defined_setting?('something_different')
90
- end
91
- end
92
-
93
- context 'setting_accessors' do
94
- should 'return the default value as fallback if :fallback => :default is given' do
95
- assert_equal 'I am a string!', User.new.a_string
96
- end
97
-
98
- should 'return the global setting value as fallback if :fallback => :global is given' do
99
- Setting.a_number = 42
100
- assert_equal 42, User.new.a_number
101
- end
102
-
103
- should 'return the given value as fallback if :fallback => VALUE is given' do
104
- assert_equal false, User.new.a_boolean
105
- end
106
- end
107
-
108
- context 'Class-Defined Settings' do
109
- should 'have the type defined in the setting_accessor call' do
110
- assert u = User.create(:first_name => 'a', :last_name => 'name', :locale => 'de')
111
- assert s = Setting.setting_record(:locale, User.first)
112
- assert_equal 'string', s.value_type.to_s
113
- end
114
-
115
- should 'not override global settings' do
116
- assert_raises(ArgumentError) do
117
- User.class_eval do
118
- setting_accessor :a_string, :type => :integer
119
- end
120
- end
121
- end
122
-
123
- should 'use value fallbacks' do
124
- assert_equal 'Oiski Poiski!', User.new.class_wise_with_value_fallback
125
- end
126
-
127
- should 'use default fallbacks' do
128
- assert_equal 'Kapitanski', User.new.class_wise_with_default_fallback
129
- end
130
- end
131
-
132
- context 'Boolean settings' do
133
- should 'respect temporary settings which are set to `false`' do
134
- user = User.new(:class_wise_truthy_boolean => false)
135
- assert_equal user.class_wise_truthy_boolean, false
136
- end
137
-
138
- should 'respect temporary settings which are set to `true`' do
139
- user = User.new(:a_boolean => true)
140
- assert_equal user.a_boolean, true
141
- end
142
- end
143
- end
@@ -1,154 +0,0 @@
1
- require_relative '../../../test_helper'
2
-
3
- class UserTest < ActiveSupport::TestCase
4
- context 'JSON serialization' do
5
- setup do
6
- @user = User.new(:a_string => 'test', :a_number => 42, :a_boolean => false)
7
- end
8
-
9
- should 'include the setting accessors' do
10
- SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
11
- assert_includes @user.as_json.keys, setting_name
12
- end
13
- end
14
-
15
- should 'contain the correct values' do
16
- SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
17
- assert_equal @user.as_json[setting_name.to_s], @user.send(setting_name)
18
- end
19
- end
20
-
21
- context 'when using as_json with option :only' do
22
- context 'set to a class attribute name' do
23
- should 'not include any setting accessors' do
24
- json = @user.as_json :only => [:first_name]
25
- SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
26
- assert_not_includes json.keys, setting_name
27
- end
28
- end
29
- end
30
-
31
- context 'set to a setting accessor name' do
32
- should 'not include other setting accessors' do
33
- json = @user.as_json :only => [:a_string]
34
- assert_includes json.keys, 'a_string'
35
- (SettingAccessors::Internal.setting_accessor_names(User) - ['a_string']).each do |setting_name|
36
- assert_not_includes json.keys, setting_name
37
- end
38
- end
39
- end
40
- end
41
-
42
- context 'when using as_json with option :except' do
43
- context 'set to a class attribute name' do
44
- should 'include all setting accessors' do
45
- json = @user.as_json :except => [:first_name]
46
- SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
47
- assert_includes json.keys, setting_name
48
- end
49
- end
50
- end
51
-
52
- context 'set to a setting accessor name' do
53
- should 'include all other setting accessors' do
54
- json = @user.as_json :except => [:a_string]
55
- assert_not_includes json.keys, 'a_string'
56
- (SettingAccessors::Internal.setting_accessor_names(User) - ['a_string']).each do |setting_name|
57
- assert_includes json.keys, setting_name
58
- end
59
- end
60
- end
61
- end
62
- end
63
-
64
- context 'Boolean getter methods' do
65
- setup do
66
- @user = User.new(:a_string => 'test', :a_number => 42, :a_boolean => false)
67
- end
68
-
69
- should 'be created for boolean settings' do
70
- assert @user.respond_to?(:a_boolean?), '?-getter is not defined for boolean settings'
71
- end
72
-
73
- should 'return the same value as the original getter' do
74
- assert_equal @user.a_boolean, @user.a_boolean?
75
- end
76
-
77
- should 'not be created for non-boolean settings' do
78
- assert !@user.respond_to?(:a_number?)
79
- assert !@user.respond_to?(:a_string?)
80
- end
81
- end
82
-
83
- context 'the read set (@temp_settings)' do
84
- setup do
85
- @user = User.create
86
- @user_alias = User.find(@user.id)
87
-
88
- # Use @user_alias here to ensure that the setting value is saved in the instance's read set
89
- @user.a_boolean = !@user_alias.a_boolean
90
- assert @user.save
91
- end
92
-
93
- should 'be refreshed with the new values on #reload' do
94
- assert @user_alias.reload
95
- assert_equal @user.a_boolean, @user_alias.a_boolean
96
- end
97
- end
98
-
99
- context 'Polymorphic class-wise settings' do
100
- setup do
101
- @user = User.create
102
- end
103
-
104
- context 'when being assigned an initial value' do
105
- should 'be created in database' do
106
- @user.polymorphic_setting = {:a => :b}
107
- assert @user.save
108
- assert_equal User.last, @user
109
- assert_equal User.last.polymorphic_setting, {:a => :b}
110
- end
111
-
112
- should 'be created in database if one of their properties changes' do
113
- @user.polymorphic_setting[:new_key] = 'new_value'
114
- assert @user.save
115
- assert_equal User.last, @user
116
- assert_equal({:new_key => 'new_value'}, User.last.polymorphic_setting)
117
- end
118
-
119
- should 'not change the value of other assignable settings' do
120
- @user2 = User.create
121
- @user2.polymorphic_setting = {:foo => :bar}
122
- assert @user2.save
123
- assert_equal User.first.polymorphic_setting, {}
124
- end
125
- end
126
-
127
- context 'when being updated' do
128
- setup do
129
- @user.polymorphic_setting = {:a => :b}
130
- assert @user.save
131
- assert @user.reload
132
- assert_equal({:a => :b}, @user.polymorphic_setting)
133
- assert_equal({:a => :b}, User.last.polymorphic_setting)
134
- end
135
-
136
- # Single hash value changed, etc.
137
- should 'be saved if one of their properties changes' do
138
- @user.polymorphic_setting[:a] = :c
139
- assert @user.save
140
- assert @user.reload
141
- assert_equal({:a => :c}, @user.polymorphic_setting)
142
- assert_equal({:a => :c}, User.last.polymorphic_setting)
143
- end
144
-
145
- should 'be updated if their whole value changes' do
146
- @user.polymorphic_setting = {:a => :c}
147
- assert @user.save
148
- assert @user.reload
149
- assert_equal({:a => :c}, @user.polymorphic_setting)
150
- assert_equal({:a => :c}, User.last.polymorphic_setting)
151
- end
152
- end
153
- end
154
- end
@@ -1,15 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- class InstallGeneratorTest < Rails::Generators::TestCase
4
- tests SettingAccessors::Generators::InstallGenerator
5
- destination File.expand_path('../dummy/tmp', File.dirname(__FILE__))
6
- setup :prepare_destination
7
-
8
- test 'Assert all files are properly created' do
9
- run_generator
10
- assert_file 'config/settings.yml'
11
- assert_file 'config/initializers/setting_accessors.rb'
12
- assert_file 'app/models/setting.rb'
13
- assert_migration 'db/migrate/create_settings.rb'
14
- end
15
- end
@@ -1,4 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class SettingAccessorsTest < ActiveSupport::TestCase
4
- end
data/test/test_helper.rb DELETED
@@ -1,31 +0,0 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
3
-
4
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
-
6
- require 'active_support'
7
- require 'minitest'
8
- require 'shoulda'
9
- require "rails/test_help"
10
-
11
- Rails.backtrace_cleaner.remove_silencers!
12
-
13
- # Load support files
14
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
-
16
- # Load fixtures from the engine
17
- if ActiveSupport::TestCase.method_defined?(:fixture_path=)
18
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
19
- end
20
-
21
- # configure shoulda matchers
22
- Shoulda::Matchers.configure do |config|
23
- config.integrate do |with|
24
- with.test_framework :minitest
25
- with.library :rails
26
- end
27
- end
28
-
29
- # For generators
30
- require "rails/generators/test_case"
31
- require "generators/setting_accessors/install_generator"