groupped_settings 0.1.2.18705
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +72 -0
- data/lib/generators/groupped/settings/install_generator.rb +29 -0
- data/lib/generators/groupped/settings/migration_generator.rb +30 -0
- data/lib/generators/templates/groupped_settings.rb +3 -0
- data/lib/generators/templates/migration.rb +30 -0
- data/lib/groupped.rb +4 -0
- data/lib/groupped/settings.rb +35 -0
- data/lib/groupped/settings/configuration.rb +19 -0
- data/lib/groupped/settings/group.rb +84 -0
- data/lib/groupped/settings/record.rb +22 -0
- data/lib/groupped/settings/settingsable.rb +23 -0
- data/lib/groupped/settings/version.rb +10 -0
- data/lib/groupped_settings.rb +4 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2bed51ae93461d3771be203dbdcbcd6dc2554ab3856833c23c080b7d1c57821f
|
4
|
+
data.tar.gz: 5bcf893e30ec6e03ea6a2a5402e1dbd010e6297fad178302895db3f19e3f2959
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 999b547cf4a69178958b0e1d5dfe20440ce44349102b3375be345aee47b42d79b30c88e82a6505e43ccbaeadb1f76564ca83f196d754095fbe4850c6c0dcba45
|
7
|
+
data.tar.gz: 7550fe33af67f8b29b6b0a484d5e4fb02c57bb552cbf435c7c77343cf0a04b90a6c3b839255b0a9b199ddc77a5e674c53723d4b7bfca4795b0617524bf819851
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Yet anoter Settings Plugin for Rails
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/groupped_settings.svg)](https://rubygems.org/gems/groupped_settings)
|
4
|
+
[![Gem](https://img.shields.io/gem/dt/groupped_settings.svg)](https://rubygems.org/gems/groupped_settings/versions)
|
5
|
+
[![YARD](https://badgen.net/badge/YARD/doc/blue)](http://www.rubydoc.info/gems/groupped_settings)
|
6
|
+
|
7
|
+
|
8
|
+
[![Coverage](https://lysander.x.rnds.pro/api/v1/badges/gs_quality.svg)](https://lysander.x.rnds.pro/api/v1/badges/gs_quality.html)
|
9
|
+
[![Quality](https://lysander.x.rnds.pro/api/v1/badges/gs_quality.svg)](https://lysander.x.rnds.pro/api/v1/badges/gs_quality.html)
|
10
|
+
[![Outdated](https://lysander.x.rnds.pro/api/v1/badges/gs_outdated.svg)](https://lysander.x.rnds.pro/api/v1/badges/gs_outdated.html)
|
11
|
+
[![Vulnerabilities](https://lysander.x.rnds.pro/api/v1/badges/gs_vulnerable.svg)](https://lysander.x.rnds.pro/api/v1/badges/gs_vulnerable.html)
|
12
|
+
|
13
|
+
|
14
|
+
Groupped::Settings is a plugin that manage groupped settings for Rails :) Settings stored in own database table as json(b) field. Splitted by groups and can have polymorphyc referece to other models.
|
15
|
+
|
16
|
+
## Setup
|
17
|
+
|
18
|
+
`rails g rake groupped_settings:install`
|
19
|
+
`rails g rake groupped:settings:migration`
|
20
|
+
|
21
|
+
Now update your database with:
|
22
|
+
|
23
|
+
`rake db:migrate`
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Define settings group class.
|
28
|
+
```ruby
|
29
|
+
class GeneralSettings < Groupped::Settings::Group
|
30
|
+
self.group_name = 'general'
|
31
|
+
|
32
|
+
attribute :identifier, :string, default: 'application'
|
33
|
+
attribute :secret, :string
|
34
|
+
|
35
|
+
validates :identifier, presence: true
|
36
|
+
validates :secret, length: { minimum: 16 }
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Using global settings:
|
41
|
+
```ruby
|
42
|
+
s = GeneralSettings.load
|
43
|
+
s.identifier = '123123123'
|
44
|
+
s.save!
|
45
|
+
|
46
|
+
s = Groupped::Settings[:general, GeneralSettings]
|
47
|
+
s.secret = '12345678987654321'
|
48
|
+
s.save!
|
49
|
+
```
|
50
|
+
|
51
|
+
|
52
|
+
Using settings fot some model:
|
53
|
+
```ruby
|
54
|
+
s = GeneralSettings.load(target: User.first)
|
55
|
+
s.identifier = '123123123'
|
56
|
+
s.save!
|
57
|
+
|
58
|
+
s = Groupped::Settings[:general, User.first]
|
59
|
+
s.secret = '12345678987654321'
|
60
|
+
s.save!
|
61
|
+
```
|
62
|
+
|
63
|
+
Include Settingsable concern:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class User
|
67
|
+
include Groupped::Settings::Settingsable
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
s = User.first.settings_group(:general, GeneralSettings)
|
72
|
+
```
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/base'
|
4
|
+
|
5
|
+
module Groupped
|
6
|
+
module Settings
|
7
|
+
module Generators
|
8
|
+
|
9
|
+
|
10
|
+
MissingORMError = Class.new(Thor::Error)
|
11
|
+
|
12
|
+
class InstallGenerator < Rails::Generators::Base
|
13
|
+
|
14
|
+
puts File.expand_path('../../templates', __dir__)
|
15
|
+
source_root File.expand_path('../../templates', __dir__)
|
16
|
+
|
17
|
+
desc 'Creates a Groupped::Settings initializer and copy locale files to your application.'
|
18
|
+
|
19
|
+
def copy_initializer
|
20
|
+
template 'groupped_settings.rb', 'config/initializers/groupped_settings.rb'
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module Groupped
|
5
|
+
module Settings
|
6
|
+
class MigrationGenerator < Rails::Generators::Base
|
7
|
+
|
8
|
+
|
9
|
+
include Rails::Generators::Migration
|
10
|
+
|
11
|
+
desc 'Generates migration for rails-settings'
|
12
|
+
source_root File.expand_path('../../templates', __dir__)
|
13
|
+
|
14
|
+
def create_migration_file
|
15
|
+
migration_template 'migration.rb', 'db/migrate/groupped_settings_migration.rb'
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.next_migration_number(dirname)
|
19
|
+
if ActiveRecord::Base.timestamped_migrations
|
20
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
21
|
+
else
|
22
|
+
format('%.3d', (current_migration_number(dirname) + 1))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
MIGRATION_BASE_CLASS = if ActiveRecord::VERSION::MAJOR >= 5
|
4
|
+
ActiveRecord::Migration[5.0]
|
5
|
+
else
|
6
|
+
ActiveRecord::Migration
|
7
|
+
end
|
8
|
+
|
9
|
+
class GrouppedSettingsMigration < MIGRATION_BASE_CLASS
|
10
|
+
|
11
|
+
def self.up
|
12
|
+
create_table :groupped_settings_records do |t|
|
13
|
+
t.string :group, index: true, null: true
|
14
|
+
|
15
|
+
if t.respond_to? :jsonb
|
16
|
+
t.jsonb :settings, null: false, default: {}
|
17
|
+
else
|
18
|
+
t.json :settings, null: false, default: {}
|
19
|
+
end
|
20
|
+
|
21
|
+
t.belongs_to :target, polymorphic: true, type: :string, index: true, null: true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.down
|
26
|
+
drop_table :groupped_settings_records
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
data/lib/groupped.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'groupped/settings/version'
|
4
|
+
require 'groupped/settings/configuration'
|
5
|
+
require 'groupped/settings/group'
|
6
|
+
require 'groupped/settings/settingsable'
|
7
|
+
|
8
|
+
module Groupped
|
9
|
+
module Settings
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
attr_accessor :config
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
self.config ||= ::Groupped::Settings::Configuration.new
|
18
|
+
|
19
|
+
class << self
|
20
|
+
|
21
|
+
def [](group, klass = Group, target: nil)
|
22
|
+
klass.new(Record.first_or_create!(group: group.downcase, target: target))
|
23
|
+
end
|
24
|
+
|
25
|
+
def configure
|
26
|
+
self.config ||= ::Groupped::Settings::Configuration.new
|
27
|
+
yield(config)
|
28
|
+
require 'groupped/settings/record'
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module Groupped
|
6
|
+
module Settings
|
7
|
+
class Configuration
|
8
|
+
|
9
|
+
attr_accessor :base, :logger
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@logger = Logger.new(STDOUT, level: Logger::INFO, progname: 'Groupped::Settings')
|
13
|
+
@base = ::ActiveRecord::Base
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Groupped
|
4
|
+
module Settings
|
5
|
+
class Group
|
6
|
+
|
7
|
+
include ActiveModel::Model
|
8
|
+
include ActiveModel::Attributes
|
9
|
+
include ActiveModel::Dirty
|
10
|
+
extend ActiveModel::Translation
|
11
|
+
include ActiveModel::Serializers::JSON
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
attr_accessor :group_name
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.attribute_names
|
20
|
+
attribute_types.keys
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.sanitize(settings)
|
24
|
+
Record.sanitize(settings).slice(*self.attribute_names)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.load(target: nil)
|
28
|
+
Groupped::Settings[self.group_name, self, target: target]
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(record)
|
32
|
+
@settings_record = record
|
33
|
+
super(self.class.sanitize(record.settings))
|
34
|
+
clear_changes_information
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_h
|
38
|
+
serializable_hash
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_hash
|
42
|
+
serializable_hash
|
43
|
+
end
|
44
|
+
|
45
|
+
def group_name
|
46
|
+
self.class.group_name
|
47
|
+
end
|
48
|
+
|
49
|
+
def ==(other)
|
50
|
+
group_name == other.group_name && attributes == other.attributes
|
51
|
+
end
|
52
|
+
|
53
|
+
def save
|
54
|
+
save!
|
55
|
+
rescue StandardError => e
|
56
|
+
false
|
57
|
+
end
|
58
|
+
|
59
|
+
def save!
|
60
|
+
validate!
|
61
|
+
_save!
|
62
|
+
clear_changes_information
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def _make_changes(changes)
|
69
|
+
changes.each_with_object({}) do |(key, (o, n)), ret|
|
70
|
+
ret[key.to_s.downcase] = n if o != n
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def _save!
|
75
|
+
@settings_record.with_lock do
|
76
|
+
@settings_record.settings.merge!(_make_changes(changes))
|
77
|
+
@settings_record.save!
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Groupped
|
4
|
+
module Settings
|
5
|
+
class Record < Groupped::Settings.config.base
|
6
|
+
|
7
|
+
self.table_name = 'groupped_settings_records'
|
8
|
+
|
9
|
+
belongs_to :target, polymorphic: true, required: false
|
10
|
+
|
11
|
+
def self.sanitize(settings)
|
12
|
+
settings.deep_transform_keys{|key| key.to_s.downcase }
|
13
|
+
end
|
14
|
+
|
15
|
+
before_validation do
|
16
|
+
self.settings = self.class.sanitize(self.settings)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Groupped
|
4
|
+
module Settings
|
5
|
+
module Settingsable
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do |_klass|
|
10
|
+
has_many :groupped_settings, as: :target, dependent: :destroy, class_name: Groupped::Settings::Record.to_s
|
11
|
+
|
12
|
+
def settings_group(group, klass = Group)
|
13
|
+
Groupped::Settings[group, klass, target: self]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class_methods do
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: groupped_settings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2.18705
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samoilenko Yuri
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.0.1
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '2.0'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.0.1
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: database_cleaner
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec_junit_formatter
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: simplecov
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: simplecov-console
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: sqlite3
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
description: Groupped::Settings is a plugin that manage groupped settings for Rails
|
146
|
+
:)
|
147
|
+
email:
|
148
|
+
- kinnalru@gmail.com
|
149
|
+
executables: []
|
150
|
+
extensions: []
|
151
|
+
extra_rdoc_files: []
|
152
|
+
files:
|
153
|
+
- README.md
|
154
|
+
- lib/generators/groupped/settings/install_generator.rb
|
155
|
+
- lib/generators/groupped/settings/migration_generator.rb
|
156
|
+
- lib/generators/templates/groupped_settings.rb
|
157
|
+
- lib/generators/templates/migration.rb
|
158
|
+
- lib/groupped.rb
|
159
|
+
- lib/groupped/settings.rb
|
160
|
+
- lib/groupped/settings/configuration.rb
|
161
|
+
- lib/groupped/settings/group.rb
|
162
|
+
- lib/groupped/settings/record.rb
|
163
|
+
- lib/groupped/settings/settingsable.rb
|
164
|
+
- lib/groupped/settings/version.rb
|
165
|
+
- lib/groupped_settings.rb
|
166
|
+
homepage: https://github.com/RnD-Soft/GrouppedSettings
|
167
|
+
licenses:
|
168
|
+
- MIT
|
169
|
+
metadata: {}
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
requirements: []
|
185
|
+
rubygems_version: 3.0.3
|
186
|
+
signing_key:
|
187
|
+
specification_version: 4
|
188
|
+
summary: Groupped::Settings is a plugin that manage groupped settings for Rails :)
|
189
|
+
test_files: []
|