smart_settings 0.1.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.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +44 -0
- data/LICENSE.txt +21 -0
- data/README.md +133 -0
- data/Rakefile +10 -0
- data/lib/generators/smart_settings/install_generator.rb +21 -0
- data/lib/generators/smart_settings/setting_generator.rb +42 -0
- data/lib/generators/smart_settings/templates/migration.rb +14 -0
- data/lib/generators/smart_settings/templates/model.rb +2 -0
- data/lib/generators/smart_settings/templates/setting.rb +5 -0
- data/lib/smart_settings.rb +11 -0
- data/lib/smart_settings/attributes.rb +57 -0
- data/lib/smart_settings/base.rb +12 -0
- data/lib/smart_settings/naming.rb +31 -0
- data/lib/smart_settings/persistence.rb +59 -0
- data/lib/smart_settings/querying.rb +63 -0
- data/lib/smart_settings/setting.rb +7 -0
- data/lib/smart_settings/version.rb +3 -0
- metadata +134 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 6c8402e60d1de130708fb6ea8717e2e2eb6304fa
|
|
4
|
+
data.tar.gz: 5a6f0e458d3e251844703d84faae453bf5bc3730
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 74a9905cd957703efbef0e0732be4b95d3097ac9fa4ac861acedb4027d3cc2e7fdc71871280951bc2d7a8cff9fa0e516e894413e1dbff7090bec592355401733
|
|
7
|
+
data.tar.gz: 8acebc30ba8e4b587b7c86c173b3e1125dfdedbc3119bb0162152c22fcaadcfd0bf14575175a536b78a8c0b24b56d757f08e77244f6c2b8d36fe00de983e0edc
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at info@hardpixel.eu. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
smart_settings (0.1.0)
|
|
5
|
+
activerecord (~> 5.0)
|
|
6
|
+
tableless (~> 0.1)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activemodel (5.1.4)
|
|
12
|
+
activesupport (= 5.1.4)
|
|
13
|
+
activerecord (5.1.4)
|
|
14
|
+
activemodel (= 5.1.4)
|
|
15
|
+
activesupport (= 5.1.4)
|
|
16
|
+
arel (~> 8.0)
|
|
17
|
+
activesupport (5.1.4)
|
|
18
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
19
|
+
i18n (~> 0.7)
|
|
20
|
+
minitest (~> 5.1)
|
|
21
|
+
tzinfo (~> 1.1)
|
|
22
|
+
arel (8.0.0)
|
|
23
|
+
concurrent-ruby (1.0.5)
|
|
24
|
+
i18n (0.9.0)
|
|
25
|
+
concurrent-ruby (~> 1.0)
|
|
26
|
+
minitest (5.10.3)
|
|
27
|
+
rake (10.5.0)
|
|
28
|
+
tableless (0.1.0)
|
|
29
|
+
activerecord (~> 5.0)
|
|
30
|
+
thread_safe (0.3.6)
|
|
31
|
+
tzinfo (1.2.3)
|
|
32
|
+
thread_safe (~> 0.1)
|
|
33
|
+
|
|
34
|
+
PLATFORMS
|
|
35
|
+
ruby
|
|
36
|
+
|
|
37
|
+
DEPENDENCIES
|
|
38
|
+
bundler (~> 1.14)
|
|
39
|
+
minitest (~> 5.0)
|
|
40
|
+
rake (~> 10.0)
|
|
41
|
+
smart_settings!
|
|
42
|
+
|
|
43
|
+
BUNDLED WITH
|
|
44
|
+
1.15.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Jonian Guveli
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# SmartSettings
|
|
2
|
+
|
|
3
|
+
Stores and retrieves settings on an ActiveRecord class, with support for application and per record settings.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/rb/smart_settings)
|
|
6
|
+
[](https://travis-ci.org/hardpixel/smart-settings)
|
|
7
|
+
[](https://codeclimate.com/github/hardpixel/smart-settings/maintainability)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'smart_settings'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install smart_settings
|
|
24
|
+
|
|
25
|
+
Then run the settings generator that will create a migration and a `Setting` model:
|
|
26
|
+
|
|
27
|
+
rails g smart_settings:install
|
|
28
|
+
|
|
29
|
+
And finally run the migrations:
|
|
30
|
+
|
|
31
|
+
rails db:migrate
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
To create an new setting you can use the setting generator. The format of the generator arguments is `name attribute:type:default:group`. To generate a setting with the name email:
|
|
36
|
+
|
|
37
|
+
rails g smart_settings:setting email sender:string:info@website.com domain:string:website.com:smtp user:string:smtp@website.com:smtp password:string::smtp
|
|
38
|
+
|
|
39
|
+
The command above will generate the `EmailSettings` class inside the `app/settings` folder:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
class EmailSettings < SmartSettings::Base
|
|
43
|
+
setting :sender, :string, default: 'info@website.com'
|
|
44
|
+
setting :domain, :string, default: 'website.com', group: :smtp
|
|
45
|
+
setting :user, :string, default: 'smtp@website.com', group: :smtp
|
|
46
|
+
setting :password, :string, group: :smtp
|
|
47
|
+
end
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Then you can use the `Setting` model or the `EmailSettings` class to get and set attributes:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
# Get email settings using the Setting model
|
|
54
|
+
email = Setting.find(:email)
|
|
55
|
+
|
|
56
|
+
# Get email settings using EmailSettings class
|
|
57
|
+
email = EmailSettings
|
|
58
|
+
|
|
59
|
+
# Get all setting attributes
|
|
60
|
+
email.all # { sender: "info@website.com", smtp: { domain: "website.com", user: "smtp@website.com", password: nil } }
|
|
61
|
+
|
|
62
|
+
# Get all setting group attributes
|
|
63
|
+
email.smtp # { domain: "website.com", user: "smtp@website.com", password: nil }
|
|
64
|
+
|
|
65
|
+
# Get setting specific attributes
|
|
66
|
+
email.sender # "info@website.com"
|
|
67
|
+
email.smtp_user # "smtp@website.com"
|
|
68
|
+
|
|
69
|
+
# Update setting attributes and save them in the settings table
|
|
70
|
+
email.update sender: "notify@website.com", smtp_user: "admin@website.com"
|
|
71
|
+
|
|
72
|
+
# Get setting updated attributes
|
|
73
|
+
email.sender # "notify@website.com"
|
|
74
|
+
email.smtp_user # "admin@website.com"
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The `Setting` model that is created with the install generator and the settings classes the are created with the setting generator, use the [tableless](https://github.com/hardpixel/tableless) gem to act like ActiveRecord models. This makes it easy to create CRUD controllers and views like you would do with any model:
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
class SettingsController < ApplicationController
|
|
81
|
+
# Show and edit actions are omitted from the example since they usually are empty
|
|
82
|
+
# New and create actions cannot be used since you cannot create new settings
|
|
83
|
+
|
|
84
|
+
before_action :set_setting, only: [:show, :edit, :update, :destroy]
|
|
85
|
+
|
|
86
|
+
def index
|
|
87
|
+
@setting = Setting.all
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def update
|
|
91
|
+
if @setting.update(setting_params)
|
|
92
|
+
redirect_to setting_path(@setting), notice: 'Setting was successfully updated.'
|
|
93
|
+
else
|
|
94
|
+
render :edit
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def destroy
|
|
99
|
+
@setting.destroy
|
|
100
|
+
redirect_to request.referrer, notice: 'Setting was successfully reset to defaults.'
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
private
|
|
104
|
+
|
|
105
|
+
def set_setting
|
|
106
|
+
@setting = Setting.find(params[:id])
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def setting_params
|
|
110
|
+
params.require(:setting).permit(@setting.permitted_attributes)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
# TODO
|
|
116
|
+
|
|
117
|
+
* Add support for record settings
|
|
118
|
+
|
|
119
|
+
## Development
|
|
120
|
+
|
|
121
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
122
|
+
|
|
123
|
+
## Contributing
|
|
124
|
+
|
|
125
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/smart-settings. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
130
|
+
|
|
131
|
+
## Code of Conduct
|
|
132
|
+
|
|
133
|
+
Everyone interacting in the SmartSettings project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hardpixel/smart-settings/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'rails/generators'
|
|
2
|
+
require 'rails/generators/migration'
|
|
3
|
+
require 'rails/generators/active_record'
|
|
4
|
+
|
|
5
|
+
module SmartSettings
|
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
|
7
|
+
include Rails::Generators::Migration
|
|
8
|
+
|
|
9
|
+
desc 'Generates migrations to add settings tables.'
|
|
10
|
+
source_root File.expand_path('../templates', __FILE__)
|
|
11
|
+
|
|
12
|
+
def create_migration_file
|
|
13
|
+
template 'model.rb', 'app/models/setting.rb'
|
|
14
|
+
migration_template 'migration.rb', 'db/migrate/create_settings.rb'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.next_migration_number(dirname)
|
|
18
|
+
::ActiveRecord::Generators::Base.next_migration_number(dirname)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require 'rails/generators'
|
|
2
|
+
|
|
3
|
+
module SmartSettings
|
|
4
|
+
class SettingGenerator < Rails::Generators::Base
|
|
5
|
+
|
|
6
|
+
desc 'Generates a new setting class in settings folder.'
|
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
|
8
|
+
|
|
9
|
+
argument :name, type: :string, required: true
|
|
10
|
+
|
|
11
|
+
def create_migration_file
|
|
12
|
+
template 'setting.rb', "app/settings/#{setting_file}.rb"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def setting_class
|
|
18
|
+
name.camelize
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def setting_file
|
|
22
|
+
"#{name.downcase.underscore}_settings"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def setting_fields
|
|
26
|
+
args.map do |arg|
|
|
27
|
+
field, cast_type, default, group = arg.split(':')
|
|
28
|
+
|
|
29
|
+
items = [":#{field.to_sym}", ":#{cast_type.to_sym}"]
|
|
30
|
+
items << ["default: #{value_type_cast(cast_type.to_sym, default)}"] if default.present?
|
|
31
|
+
items << ["group: :#{group.to_sym}"] if group.present?
|
|
32
|
+
|
|
33
|
+
items.join(', ')
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def value_type_cast(cast_type, value)
|
|
38
|
+
strings = [:string, :date, :datetime, :time]
|
|
39
|
+
cast_type.in?(strings) ? "'#{value}'" : value
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class CreateSettings < ActiveRecord::Migration[5.0]
|
|
2
|
+
def change
|
|
3
|
+
create_table :settings do |t|
|
|
4
|
+
t.string :var, null: false
|
|
5
|
+
t.string :value
|
|
6
|
+
t.integer :settable_id
|
|
7
|
+
t.string :settable_type, null: false
|
|
8
|
+
|
|
9
|
+
t.timestamps
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
add_index :settings, [:settable_id, :settable_type, :var], unique: true
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require 'active_support'
|
|
2
|
+
require 'smart_settings/version'
|
|
3
|
+
require 'smart_settings/naming'
|
|
4
|
+
require 'smart_settings/attributes'
|
|
5
|
+
require 'smart_settings/querying'
|
|
6
|
+
require 'smart_settings/persistence'
|
|
7
|
+
require 'smart_settings/base'
|
|
8
|
+
require 'smart_settings/setting'
|
|
9
|
+
|
|
10
|
+
module SmartSettings
|
|
11
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module SmartSettings
|
|
2
|
+
module Attributes
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
class_attribute :setting_names
|
|
7
|
+
class_attribute :setting_groups
|
|
8
|
+
|
|
9
|
+
self.setting_names = []
|
|
10
|
+
self.setting_groups = {}
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class_methods do
|
|
14
|
+
def setting(name, type_cast=:string, options={})
|
|
15
|
+
gname = options.delete(:group)
|
|
16
|
+
sname = gname.nil? ? name : :"#{gname}_#{name}"
|
|
17
|
+
|
|
18
|
+
attribute sname, type_cast, options
|
|
19
|
+
self.setting_names += [sname]
|
|
20
|
+
|
|
21
|
+
unless gname.nil?
|
|
22
|
+
group = setting_groups.fetch(gname, []) + [sname]
|
|
23
|
+
self.setting_groups = setting_groups.merge(gname => group)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def method_missing(method, *args, &block)
|
|
28
|
+
setting_names.any? ? new.send(method, *args, &block) : super
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def all
|
|
33
|
+
except = setting_groups.values.flatten + [:var]
|
|
34
|
+
single = attributes.symbolize_keys.except(*except)
|
|
35
|
+
groups = Hash[setting_groups.keys.map { |s| [s, group(s)] }]
|
|
36
|
+
|
|
37
|
+
single.merge(groups)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def group_exists?(name)
|
|
41
|
+
setting_groups.keys.include? name
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def group(name)
|
|
45
|
+
if group_exists?(name)
|
|
46
|
+
keys = setting_groups[name]
|
|
47
|
+
data = attributes.symbolize_keys.select { |k, _v| k.in? keys }
|
|
48
|
+
|
|
49
|
+
Hash[data.map { |k, v| [k.to_s.sub("#{name}_", '').to_sym, v] }]
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def method_missing(method, *args, &block)
|
|
54
|
+
group_exists?(method) ? group(method) : super
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module SmartSettings
|
|
2
|
+
module Naming
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
attribute :var, :string
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class_methods do
|
|
10
|
+
def model_name
|
|
11
|
+
ActiveModel::Name.new(self, nil, 'Setting')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def permitted_attributes
|
|
15
|
+
attribute_names
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def var
|
|
20
|
+
"#{self.class.name}".demodulize.sub('Settings', '')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_param
|
|
24
|
+
"#{var}".parameterize
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def permitted_attributes
|
|
28
|
+
attribute_names - ['var']
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module SmartSettings
|
|
2
|
+
module Persistence
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
after_initialize do
|
|
7
|
+
self.var = var
|
|
8
|
+
|
|
9
|
+
settings.each do |setting|
|
|
10
|
+
svar, value = [setting.var, setting.value]
|
|
11
|
+
send(:"#{svar}=", cast_setting_value(svar, value))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
@_suspend_save_callbacks = true
|
|
15
|
+
self.save
|
|
16
|
+
@_suspend_save_callbacks = false
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
before_save do
|
|
20
|
+
unless @_suspend_save_callbacks
|
|
21
|
+
changes_to_save.each do |var, value|
|
|
22
|
+
create_or_update_setting(var, value.last)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
before_destroy do
|
|
28
|
+
settings.destroy_all
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def settings
|
|
33
|
+
Setting.where(settable_type: self.class.name, settable_id: id)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def cast_setting_value(var, value)
|
|
39
|
+
self.class.attribute_types[var].cast(value)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def create_setting(var, value)
|
|
43
|
+
parameters = { var: var, value: cast_setting_value(var, value) }
|
|
44
|
+
Setting.create(parameters.merge(settable_type: self.class.name, settable_id: id))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def update_setting(var, value)
|
|
48
|
+
settings.where(var: var).update(value: cast_setting_value(var, value))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def create_or_update_setting(var, value)
|
|
52
|
+
if settings.where(var: var).exists?
|
|
53
|
+
update_setting(var, value)
|
|
54
|
+
else
|
|
55
|
+
create_setting(var, value)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module SmartSettings
|
|
2
|
+
module Querying
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
class_methods do
|
|
6
|
+
def setting_class(var)
|
|
7
|
+
"#{var}_settings".camelize
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def find(*var)
|
|
11
|
+
if var.size > 1
|
|
12
|
+
results = where(var: var)
|
|
13
|
+
raise_record_not_found_exception!(var) if results.blank?
|
|
14
|
+
else
|
|
15
|
+
find_by_var!(var.first)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def find_by_var(var)
|
|
20
|
+
setting = setting_class(var).safe_constantize
|
|
21
|
+
setting.nil? ? nil : setting.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def find_by_var!(var)
|
|
25
|
+
setting = find_by_var(var)
|
|
26
|
+
setting.nil? ? raise_record_not_found_exception!(var) : setting
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def where(options={})
|
|
30
|
+
vars = Hash[options][:var]
|
|
31
|
+
return all if vars.nil?
|
|
32
|
+
|
|
33
|
+
classes = Array(vars).map { |var| setting_class(var).safe_constantize }
|
|
34
|
+
classes.reject(&:nil?).map { |setting| setting.new }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def all
|
|
38
|
+
if setting_names.empty?
|
|
39
|
+
files = Dir.glob(Rails.root.join('app', 'settings', '*.rb'))
|
|
40
|
+
files = files.map { |file| file.to_s.split('/').last.sub('_settings.rb', '') }
|
|
41
|
+
|
|
42
|
+
where(var: files)
|
|
43
|
+
else
|
|
44
|
+
new.all
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def method_missing(method, *args, &block)
|
|
49
|
+
setting = find_by_var(method)
|
|
50
|
+
setting.nil? ? super : setting
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def raise_record_not_found_exception!(ids)
|
|
56
|
+
vars = ids.is_a?(Array) ? "(#{ids.join(', ')})" : ids
|
|
57
|
+
error = "Couldn't find Settings with var: #{vars}"
|
|
58
|
+
|
|
59
|
+
raise ActiveRecord::RecordNotFound.new(error, 'Settings', 'var')
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: smart_settings
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jonian Guveli
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-10-25 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: :runtime
|
|
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: tableless
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0.1'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0.1'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.14'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.14'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: minitest
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '5.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '5.0'
|
|
83
|
+
description: Stores and retrieves settings on an ActiveRecord class, with support
|
|
84
|
+
for application and per record settings.
|
|
85
|
+
email:
|
|
86
|
+
- jonian@hardpixel.eu
|
|
87
|
+
executables: []
|
|
88
|
+
extensions: []
|
|
89
|
+
extra_rdoc_files: []
|
|
90
|
+
files:
|
|
91
|
+
- CODE_OF_CONDUCT.md
|
|
92
|
+
- Gemfile
|
|
93
|
+
- Gemfile.lock
|
|
94
|
+
- LICENSE.txt
|
|
95
|
+
- README.md
|
|
96
|
+
- Rakefile
|
|
97
|
+
- lib/generators/smart_settings/install_generator.rb
|
|
98
|
+
- lib/generators/smart_settings/setting_generator.rb
|
|
99
|
+
- lib/generators/smart_settings/templates/migration.rb
|
|
100
|
+
- lib/generators/smart_settings/templates/model.rb
|
|
101
|
+
- lib/generators/smart_settings/templates/setting.rb
|
|
102
|
+
- lib/smart_settings.rb
|
|
103
|
+
- lib/smart_settings/attributes.rb
|
|
104
|
+
- lib/smart_settings/base.rb
|
|
105
|
+
- lib/smart_settings/naming.rb
|
|
106
|
+
- lib/smart_settings/persistence.rb
|
|
107
|
+
- lib/smart_settings/querying.rb
|
|
108
|
+
- lib/smart_settings/setting.rb
|
|
109
|
+
- lib/smart_settings/version.rb
|
|
110
|
+
homepage: https://github.com/hardpixel/smart-settings
|
|
111
|
+
licenses:
|
|
112
|
+
- MIT
|
|
113
|
+
metadata: {}
|
|
114
|
+
post_install_message:
|
|
115
|
+
rdoc_options: []
|
|
116
|
+
require_paths:
|
|
117
|
+
- lib
|
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
|
+
requirements:
|
|
120
|
+
- - ">="
|
|
121
|
+
- !ruby/object:Gem::Version
|
|
122
|
+
version: '0'
|
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
|
+
requirements:
|
|
125
|
+
- - ">="
|
|
126
|
+
- !ruby/object:Gem::Version
|
|
127
|
+
version: '0'
|
|
128
|
+
requirements: []
|
|
129
|
+
rubyforge_project:
|
|
130
|
+
rubygems_version: 2.6.13
|
|
131
|
+
signing_key:
|
|
132
|
+
specification_version: 4
|
|
133
|
+
summary: Persist application or record settings on ActiveRecord
|
|
134
|
+
test_files: []
|