groupped_settings 0.2.1.18711 → 0.2.2.67551

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
  SHA256:
3
- metadata.gz: 98b5d72c5fe3fdeaceee36e13270a27a5f26ad5d73e5240c4b6a54966125055e
4
- data.tar.gz: 67410e1173be82008f85ae66cca623ae0d8307c3a6d613e67c3a4ca04081bf41
3
+ metadata.gz: c2b3289e460b363068beab55a9a7512fe7bd46680a0a353132ed1cd94c2a2793
4
+ data.tar.gz: df3b8a4790c1c30a8979d52574c20a571bfe33b6b66dd4fb0229aed9fc706eae
5
5
  SHA512:
6
- metadata.gz: 581e8feb560ef7711c1e0e102b9b2788fc9c6ba2699caa576219684e26a1d3b6f2488a0c889ad355b4e3e52f25ab441c3d00ea6a2976efc7580dee9027784aee
7
- data.tar.gz: 1ac8925ecab3499923ef7920e62d863bedc9f7961db9906e19bab391ab434f7638a10a8e9d73a2cf90f1ee59547985dd4512ab4ee448b93400d9a5ce24c60b1d
6
+ metadata.gz: 5575a50cc7d0e47a5f36f5874028d7c67ebd3d0b57cebe97a33d7d2db378eb43db6d41582c6ef1fe9916086a3220ce18ca038ab95e21aef8436535634a2308d2
7
+ data.tar.gz: 2b79aa4b9b8e81547263e46345f74e5f2593038c4ca795b9f4c4ce70adffe34de74ef200ccf5e6085214ecce5aff3d62d8cf2ea8701b3d6600ad4c2c4d84fcdf
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ Samoilenko Yuri <kinnalru _at_ gmail.com>
@@ -24,10 +24,12 @@ module Groupped
24
24
  Record.sanitize(settings).slice(*self.attribute_names)
25
25
  end
26
26
 
27
- def self.load(target: nil)
28
- Groupped::Settings[self.group_name, self, target: target]
27
+ def self.load(target: nil, settings: {})
28
+ Groupped::Settings[self.group_name, self, target: target, settings: settings]
29
29
  end
30
30
 
31
+ attr_reader :settings_record
32
+
31
33
  def initialize(record)
32
34
  @settings_record = record
33
35
  super(self.class.sanitize(record.settings))
@@ -3,7 +3,7 @@
3
3
  module Groupped
4
4
  module Settings
5
5
 
6
- VERSION = '0.2.1'
6
+ VERSION = '0.2.2'
7
7
 
8
8
  end
9
9
  end
@@ -18,8 +18,8 @@ module Groupped
18
18
 
19
19
  class << self
20
20
 
21
- def [](group, klass = Group, target: nil)
22
- klass.new(Record.first_or_create!(group: group.downcase, target: target))
21
+ def [](group, klass = Group, target: nil, settings: {})
22
+ klass.new(Record.where(group: group.downcase, target: target).first_or_create!(settings: settings))
23
23
  end
24
24
 
25
25
  def configure
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Groupped::Settings::Group, type: :model do
4
+ class GenericGroup < Groupped::Settings::Group
5
+
6
+ self.group_name = 'generic'
7
+
8
+ attribute :key1, :integer
9
+ attribute :key2, :string, default: 'def'
10
+ attribute :key3, :string
11
+
12
+ validates :key2, length: { is: 3 }
13
+
14
+ end
15
+
16
+ describe GenericGroup do
17
+ let(:settings){ { key1: 1 } }
18
+ let(:record){ double(Groupped::Settings::Record, settings: settings) }
19
+
20
+ subject(:group){ described_class.new(record) }
21
+
22
+ it '::attribute_names should be ok' do
23
+ expect(described_class.attribute_names).to match(%w[key1 key2 key3])
24
+ end
25
+
26
+ it '::sanitize should transform keys' do
27
+ input = {
28
+ absent_key: '0',
29
+ key1: '1',
30
+ KEY2: '2',
31
+ 'KeY3' => '3'
32
+ }
33
+
34
+ expect(described_class.sanitize(input)).to match('key1' => '1', 'key2' => '2', 'key3' => '3')
35
+ end
36
+
37
+ describe 'instance' do
38
+ before { expect(record).to receive(:settings).and_return(settings) }
39
+
40
+ it { expect(described_class).to receive(:sanitize).and_call_original; subject }
41
+ it { is_expected.to include('key1' => 1, 'key2' => 'def', 'key3' => nil) }
42
+ it { is_expected.to be_valid }
43
+ it { expect(group.changes).to be_empty }
44
+
45
+ describe '#save!' do
46
+ let(:save){ group.save! }
47
+
48
+ before { expect(record).to receive(:with_lock).and_yield }
49
+ before { expect(record).to receive(:save!).and_return(true) }
50
+
51
+ it { is_expected.to receive(:validate!).and_call_original; save }
52
+ it { expect(save).to eq(true) }
53
+
54
+ it do
55
+ group.key1 = 2
56
+ expect{ save }.to change{ group.changes }.from('key1' => [1, 2]).to({})
57
+ end
58
+ end
59
+
60
+
61
+ describe '#save' do
62
+ let(:save){ group.save; }
63
+
64
+ it ('success is expected to eq true') { is_expected.to receive(:save!).and_return(true); expect(save).to eq(true) }
65
+ it ('exception is expected to eq false'){ is_expected.to receive(:save!).and_raise('error'); expect(save).to eq(false) }
66
+ end
67
+
68
+ it '#to_h' do
69
+ expect(group.to_h).to match('key1' => 1, 'key2' => 'def', 'key3' => nil)
70
+ end
71
+
72
+ it '#to_hash' do
73
+ expect(group.to_h).to match('key1' => 1, 'key2' => 'def', 'key3' => nil)
74
+ end
75
+
76
+ describe 'validation' do
77
+ let(:settings){ { key1: 1, key2: '123123123' } }
78
+
79
+ it { is_expected.to include('key1' => 1, 'key2' => '123123123', 'key3' => nil) }
80
+ it { is_expected.not_to be_valid }
81
+ it { expect(group.changes).to be_empty }
82
+
83
+ describe '#save!' do
84
+ let(:save){ group.save! }
85
+
86
+ it { expect{ save }.to raise_error(ActiveModel::ValidationError) }
87
+ end
88
+
89
+ describe '#save' do
90
+ let(:save){ group.save }
91
+
92
+ it { expect(save).to eq(false) }
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Groupped::Settings, type: :model do
4
+ describe '#[]' do
5
+ let(:group){ :test }
6
+ subject{ described_class[group] }
7
+
8
+ it { is_expected.to be_an(Groupped::Settings::Group) }
9
+ it {
10
+ expect{ subject }.to change{ Groupped::Settings::Record.where(group: group).count }
11
+ .from(0)
12
+ .to(1)
13
+ }
14
+ end
15
+
16
+ class TestGroup1 < Groupped::Settings::Group
17
+
18
+ self.group_name = :test1
19
+
20
+ attribute :key1, :integer
21
+ attribute :key2, :string, default: 'def'
22
+
23
+ validates :key2, length: { is: 3 }
24
+
25
+ end
26
+
27
+ class TestGroup2 < Groupped::Settings::Group
28
+
29
+ self.group_name = :test2
30
+
31
+ attribute :yek1, :datetime, default: 10.years.ago
32
+ attribute :yek2, :string
33
+
34
+ validates :yek2, presence: true
35
+
36
+ end
37
+
38
+ groups = { test1: TestGroup1, test2: TestGroup2 }
39
+
40
+ describe 'control' do
41
+ groups.each do |(group, klass)|
42
+ describe "group: #{group} -> #{klass}" do
43
+ it { expect(klass.group_name).to eq(group) }
44
+
45
+ it { expect(Groupped::Settings[group]).to be_an(Groupped::Settings::Group) }
46
+ it { expect(Groupped::Settings[:test1, klass]).to be_an(klass) }
47
+ it { expect(Groupped::Settings[:test1, klass].group_name).to eq(group) }
48
+ it { expect(klass.load).to be_an(klass) }
49
+ it { expect(klass.load.group_name).to eq(group) }
50
+ end
51
+ end
52
+ end
53
+
54
+ it 'huge integration' do
55
+ expect(Groupped::Settings::Record.count).to eq(0)
56
+
57
+ # groups.each do
58
+ s = TestGroup1.load
59
+ expect(s.changes).to be_empty
60
+ expect(s).to include('key1' => nil, 'key2' => 'def')
61
+ expect(Groupped::Settings::Record.count).to eq(1)
62
+ expect(Groupped::Settings::Record.find_by_group(:test1).settings).to match({})
63
+
64
+ s.save!
65
+ expect(s.changes).to be_empty
66
+ expect(s).to include('key1' => nil, 'key2' => 'def')
67
+ expect(Groupped::Settings::Record.count).to eq(1)
68
+ expect(Groupped::Settings::Record.find_by_group(:test1).settings).to match({})
69
+
70
+ s.key1 = 123
71
+ expect(s.changes).not_to be_empty
72
+ expect(s).to include('key1' => 123, 'key2' => 'def')
73
+ s.save!
74
+ expect(Groupped::Settings::Record.count).to eq(1)
75
+ expect(Groupped::Settings::Record.find_by_group(:test1).settings).to match('key1' => 123)
76
+
77
+ s = TestGroup1.load
78
+ expect(s.changes).to be_empty
79
+ expect(s).to include('key1' => 123, 'key2' => 'def')
80
+ expect(Groupped::Settings::Record.count).to eq(1)
81
+ expect(Groupped::Settings::Record.find_by_group(:test1).settings).to match('key1' => 123)
82
+
83
+ # handle missing keys
84
+ r = Groupped::Settings::Record.find_by_group(:test1)
85
+ r.settings[:absent] = true
86
+ r.save!
87
+
88
+ s = TestGroup1.load
89
+ expect(s.changes).to be_empty
90
+ expect(s).to include('key1' => 123, 'key2' => 'def')
91
+ expect(Groupped::Settings::Record.count).to eq(1)
92
+ expect(Groupped::Settings::Record.find_by_group(:test1).settings).to match('key1' => 123, 'absent' => true)
93
+
94
+ s2 = TestGroup1.load
95
+
96
+ s.key1 = 321
97
+ s2.key2 = 'tes'
98
+ s2.save!
99
+
100
+ expect(Groupped::Settings::Record.find_by_group(:test1).settings).to match('key1' => 123, 'absent' => true, 'key2' => 'tes')
101
+ s.save!
102
+ expect(Groupped::Settings::Record.find_by_group(:test1).settings).to match('key1' => 321, 'absent' => true, 'key2' => 'tes')
103
+
104
+ s1 = TestGroup1.load
105
+ s2 = TestGroup1.load
106
+
107
+ expect(s1).to match(s2)
108
+
109
+ s1.key1 = 543
110
+ expect(s1).not_to match(s2)
111
+ end
112
+
113
+ describe 'with target' do
114
+ let!(:user){ User.create! }
115
+ before do
116
+ User.include Groupped::Settings::Settingsable
117
+ end
118
+
119
+ it do
120
+ expect(user.groupped_settings.count).to eq(0)
121
+ s = user.settings_group(:base)
122
+ expect(user.groupped_settings.count).to eq(1)
123
+
124
+ s = user.settings_group(:base, TestGroup1)
125
+ s.key1 = 777
126
+ s.save
127
+ expect(user.groupped_settings.count).to eq(1)
128
+ expect(user.groupped_settings.first.settings).to match('key1' => 777)
129
+ end
130
+ end
131
+ end
132
+
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by the `rails generate rspec:install` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
6
+ # this file to always be loaded, without a need to explicitly require it in any
7
+ # files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need
15
+ # it.
16
+ #
17
+ # The `.rspec` file also contains a few flags that are not defaults but that
18
+ # users commonly want.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+
22
+ # require 'rspec/retry'
23
+
24
+ require 'simplecov'
25
+ require 'simplecov-console'
26
+ ENV['RACK_ENV'] ||= 'test'
27
+
28
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
29
+ SimpleCov::Formatter::HTMLFormatter, # for gitlab
30
+ SimpleCov::Formatter::Console # for developers
31
+ ])
32
+ SimpleCov.start
33
+
34
+ $root = File.join(File.dirname(__dir__), 'spec')
35
+ Dir[File.join(__dir__, 'support', '**', '*.rb')].sort.each {|f| puts f; require f }
36
+
37
+ require 'groupped_settings'
38
+
39
+ RSpec.configure do |config|
40
+ # config.verbose_retry = true
41
+ # Try twice (retry once)
42
+ # config.default_retry_count = 2
43
+
44
+ config.before(:suite) do
45
+ Groupped::Settings.configure do |config|
46
+ end
47
+ end
48
+
49
+ # rspec-expectations config goes here. You can use an alternate
50
+ # assertion/expectation library such as wrong or the stdlib/minitest
51
+ # assertions if you prefer.
52
+ config.expect_with :rspec do |expectations|
53
+ # This option will default to `true` in RSpec 4. It makes the `description`
54
+ # and `failure_message` of custom matchers include text for helper methods
55
+ # defined using `chain`, e.g.:
56
+ # be_bigger_than(2).and_smaller_than(4).description
57
+ # # => "be bigger than 2 and smaller than 4"
58
+ # ...rather than:
59
+ # # => "be bigger than 2"
60
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
61
+ end
62
+
63
+ # rspec-mocks config goes here. You can use an alternate test double
64
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
65
+ config.mock_with :rspec do |mocks|
66
+ # Prevents you from mocking or stubbing a method that does not exist on
67
+ # a real object. This is generally recommended, and will default to
68
+ # `true` in RSpec 4.
69
+ mocks.verify_partial_doubles = true
70
+ end
71
+
72
+ # config.filter_run :molot
73
+ config.run_all_when_everything_filtered = true
74
+
75
+ # The settings below are suggested to provide a good initial experience
76
+ # with RSpec, but feel free to customize to your heart's content.
77
+ # # These two settings work together to allow you to limit a spec run
78
+ # # to individual examples or groups you care about by tagging them with
79
+ # # `:focus` metadata. When nothing is tagged with `:focus`, all examples
80
+ # # get run.
81
+ # config.filter_run :focus
82
+ # config.run_all_when_everything_filtered = true
83
+ #
84
+ # # Limits the available syntax to the non-monkey patched syntax that is
85
+ # # recommended. For more details, see:
86
+ # # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
87
+ # # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
88
+ # # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
89
+ # config.disable_monkey_patching!
90
+ #
91
+ # # Many RSpec users commonly either run the entire suite or an individual
92
+ # # file, and it's useful to allow more verbose output when running an
93
+ # # individual spec file.
94
+ # if config.files_to_run.one?
95
+ # # Use the documentation formatter for detailed output,
96
+ # # unless a formatter has already been configured
97
+ # # (e.g. via a command-line flag).
98
+ # config.default_formatter = 'doc'
99
+ # end
100
+ #
101
+ # # Print the 10 slowest examples and example groups at the
102
+ # # end of the spec run, to help surface which specs are running
103
+ # # particularly slow.
104
+ # config.profile_examples = 10
105
+ #
106
+ # # Run specs in random order to surface order dependencies. If you find an
107
+ # # order dependency and want to debug it, you can fix the order by providing
108
+ # # the seed, which is printed after each run.
109
+ # # --seed 1234
110
+ # config.order = :random
111
+ #
112
+ # # Seed global randomization in this process using the `--seed` CLI option.
113
+ # # Setting this allows you to use `--seed` to deterministically reproduce
114
+ # # test failures related to randomization by passing the same `--seed` value
115
+ # # as the one that triggered the failure.
116
+ # Kernel.srand config.seed
117
+ end
118
+
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -0,0 +1,24 @@
1
+ require 'database_cleaner'
2
+
3
+ RSpec.configure do |config|
4
+ config.before(:suite) do
5
+ DatabaseCleaner.clean_with(:truncation)
6
+ end
7
+
8
+ config.before(:all) do
9
+ DatabaseCleaner.clean_with(:truncation)
10
+ end
11
+
12
+ config.before(:each) do
13
+ DatabaseCleaner.strategy = :transaction
14
+ end
15
+
16
+ config.before(:each) do
17
+ DatabaseCleaner.start
18
+ end
19
+
20
+ config.after(:each) do
21
+ DatabaseCleaner.clean
22
+ end
23
+ end
24
+
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'protected_attributes' if ENV['PROTECTED_ATTRIBUTES'] == 'true'
5
+
6
+ I18n.enforce_available_locales = false if I18n.respond_to?(:enforce_available_locales=)
7
+
8
+ class User < ActiveRecord::Base
9
+ # has_settings do |s|
10
+ # s.key :dashboard, :defaults => { :theme => 'blue', :view => 'monthly', :filter => true }
11
+ # s.key :calendar, :defaults => { :scope => 'company'}
12
+ # end
13
+ end
14
+
15
+ def setup_db
16
+ ActiveRecord::Base.configurations = YAML.load_file(File.dirname(__FILE__) + '/database.yml')
17
+ ActiveRecord::Base.establish_connection(:test)
18
+ ActiveRecord::Migration.verbose = false
19
+
20
+ print "Testing with ActiveRecord #{ActiveRecord::VERSION::STRING}"
21
+ if ActiveRecord::VERSION::MAJOR == 4
22
+ print " #{defined?(ProtectedAttributes) ? 'with' : 'without'} gem `protected_attributes`"
23
+ end
24
+ puts
25
+
26
+ require File.expand_path("#{$root}/../lib/generators/templates/migration.rb", __FILE__)
27
+ GrouppedSettingsMigration.migrate(:up)
28
+
29
+ ActiveRecord::Schema.define(version: 1) do
30
+ create_table :users do |t|
31
+ t.string :type
32
+ t.string :name
33
+ end
34
+ end
35
+ end
36
+
37
+ def clear_db
38
+ User.delete_all
39
+ # RailsSettings::SettingObject.delete_all
40
+ end
41
+
42
+ setup_db
43
+
metadata CHANGED
@@ -1,49 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groupped_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1.18711
4
+ version: 0.2.2.67551
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samoilenko Yuri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-06 00:00:00.000000000 Z
11
+ date: 2021-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activerecord
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.1
20
23
  type: :development
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
25
28
  - !ruby/object:Gem::Version
26
- version: '5.0'
29
+ version: '2.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.1
27
33
  - !ruby/object:Gem::Dependency
28
- name: bundler
34
+ name: activerecord
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - "~>"
32
38
  - !ruby/object:Gem::Version
33
- version: '2.0'
34
- - - ">="
35
- - !ruby/object:Gem::Version
36
- version: 2.0.1
39
+ version: '5.0'
37
40
  type: :development
38
41
  prerelease: false
39
42
  version_requirements: !ruby/object:Gem::Requirement
40
43
  requirements:
41
44
  - - "~>"
42
45
  - !ruby/object:Gem::Version
43
- version: '2.0'
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: 2.0.1
46
+ version: '5.0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: database_cleaner
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +150,7 @@ executables: []
150
150
  extensions: []
151
151
  extra_rdoc_files: []
152
152
  files:
153
+ - AUTHORS
153
154
  - README.md
154
155
  - lib/generators/groupped/settings/install_generator.rb
155
156
  - lib/generators/groupped/settings/migration_generator.rb
@@ -163,6 +164,12 @@ files:
163
164
  - lib/groupped/settings/settingsable.rb
164
165
  - lib/groupped/settings/version.rb
165
166
  - lib/groupped_settings.rb
167
+ - spec/group_spec.rb
168
+ - spec/settings_spec.rb
169
+ - spec/spec_helper.rb
170
+ - spec/support/database.yml
171
+ - spec/support/database_cleaner.rb
172
+ - spec/support/db_helper.rb
166
173
  homepage: https://github.com/RnD-Soft/groupped_settings
167
174
  licenses:
168
175
  - MIT
@@ -186,4 +193,10 @@ rubygems_version: 3.0.3
186
193
  signing_key:
187
194
  specification_version: 4
188
195
  summary: Groupped::Settings is a plugin that manage groupped settings for Rails :)
189
- test_files: []
196
+ test_files:
197
+ - spec/spec_helper.rb
198
+ - spec/settings_spec.rb
199
+ - spec/support/db_helper.rb
200
+ - spec/support/database.yml
201
+ - spec/support/database_cleaner.rb
202
+ - spec/group_spec.rb