settingson 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -59
- data/app/models/concerns/settingson/base.rb +29 -6
- data/lib/settingson/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0752abcd5f9b547030d3b6dba21da1eb6b089154
|
4
|
+
data.tar.gz: c0225eecd8deae31e29b2c53998b82f4fb55618e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 847c4da786c0c542715c77496a21a5deafe565a134d1473859b4d3a7b421763f6e2ee02dee4f85535d86840feee4f098c93d781b52c43f7b63a86250e7ed99a4
|
7
|
+
data.tar.gz: b7627dac777a30f9c2be130596b6a3b7825e2a915ca78c89dbf253cfbc66abb8172a7bde7d2101e533cf4ef671d0ad2193a32bd42614d51ff12151324da9f1db
|
data/README.md
CHANGED
@@ -2,49 +2,78 @@
|
|
2
2
|
|
3
3
|
# Settingson
|
4
4
|
|
5
|
-
|
5
|
+
Simple settings management for applications (Ruby on Rails 4 with ActiveRecord)
|
6
6
|
|
7
|
+
## Example
|
8
|
+
|
9
|
+
shell commands:
|
10
|
+
```console
|
11
|
+
rails g settingson Settings
|
12
|
+
rake db:migrate
|
13
|
+
```
|
14
|
+
|
15
|
+
code:
|
7
16
|
```ruby
|
8
17
|
Settings.server.host = '127.0.0.1'
|
9
18
|
Settings.server.port = '8888'
|
10
19
|
|
11
|
-
Settings.server.host
|
12
|
-
Settings.server.port
|
20
|
+
Settings.server.host # => '127.0.0.1'
|
21
|
+
Settings.server.port # => '8888'
|
13
22
|
|
14
23
|
Settings.server.smtp.host = '127.0.0.1'
|
15
24
|
Settings.server.smtp.port = 25
|
16
25
|
|
17
|
-
Settings.server.smtp.host
|
18
|
-
Settings.server.smtp.port
|
26
|
+
Settings.server.smtp.host # => "127.0.0.1"
|
27
|
+
Settings.server.smtp.port # => 25
|
19
28
|
|
20
|
-
|
21
|
-
Settings.
|
22
|
-
Settings.rules['1st RULE'] # => "You do not talk about FIGHT CLUB."
|
29
|
+
Settings.from_hash({hello: :world})
|
30
|
+
Settings.hello # => :world
|
23
31
|
|
24
|
-
#
|
25
|
-
Settings.
|
26
|
-
Settings.
|
32
|
+
Settings.not_found # => ""
|
33
|
+
Settings.not_found.nil? # => true
|
34
|
+
Settings.not_found.empty? # => true
|
35
|
+
Settings.not_found.blank? # => true
|
36
|
+
Settings.not_found.present? # => false
|
27
37
|
|
28
|
-
#
|
29
|
-
Settings.
|
30
|
-
Settings.array_of.hashes # => [{:hello=>:world}, {"glad"=>:to}, {:see=>"you"}]
|
31
|
-
|
32
|
-
# Boolean
|
33
|
-
Settings.item # => false
|
34
|
-
Settings.item = :hello
|
35
|
-
Settings.item # => true
|
36
|
-
|
37
|
-
# ActiveRecord
|
38
|
-
Settings.item! # => <Settings id: 1, key: "item", value: :hello, created_at: "...", updated_at: "...">
|
38
|
+
# but
|
39
|
+
Settings.not_found.class # => Settings(id: integer, key: string, value: text, created_at: datetime, updated_at: datetime)
|
39
40
|
```
|
40
41
|
|
41
42
|
### Using with [Simple Form](https://github.com/plataformatec/simple_form) and [Haml](https://github.com/haml/haml)
|
42
43
|
in view:
|
43
44
|
```ruby
|
44
|
-
= simple_form_for(Settings, url: settings_path) do |f|
|
45
|
+
= simple_form_for( Settings, url: settings_path, method: :patch ) do |f|
|
45
46
|
= f.error_notification
|
46
|
-
= f.input :'host.
|
47
|
-
= f.
|
47
|
+
= f.input :'server.smtp.host', label: 'SMTP Host', as: :string, placeholder: 'mail.google.com'
|
48
|
+
= f.input :'server.smtp.port', label: 'SMTP Port', as: :string, placeholder: '25'
|
49
|
+
= f.button :submit, t('update', default: 'Update settings')
|
50
|
+
```
|
51
|
+
|
52
|
+
in controller:
|
53
|
+
```ruby
|
54
|
+
class SettingsController < ApplicationController
|
55
|
+
def update
|
56
|
+
if Settings.from_hash(params[:settings])
|
57
|
+
flash[:notice] = t('settings_updated', default: 'Settings updated successfully')
|
58
|
+
else
|
59
|
+
flash[:alert] = t('settings_not_updated', default: 'Settings not updated')
|
60
|
+
end
|
61
|
+
render :edit
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
## The initial values
|
67
|
+
in config/initializers/settingson.rb
|
68
|
+
```ruby
|
69
|
+
Rails.application.config.after_initialize do
|
70
|
+
begin
|
71
|
+
Settings.server.smtp.host? || Settings.server.smtp.host = 'host'
|
72
|
+
Settings.server.smtp.port? || Settings.server.smtp.port = '25'
|
73
|
+
rescue
|
74
|
+
Rails.logger.warn('Something goes wrong')
|
75
|
+
end
|
76
|
+
end
|
48
77
|
```
|
49
78
|
|
50
79
|
## Installation
|
@@ -72,44 +101,13 @@ Or install it yourself as:
|
|
72
101
|
```console
|
73
102
|
rails g settingson MODEL
|
74
103
|
```
|
75
|
-
Replace MODEL by the class name used for the applications settings, it's frequently `Settings` but
|
76
|
-
|
77
|
-
Next, you'll usually run
|
78
|
-
```console
|
79
|
-
rake db:migrate
|
80
|
-
```
|
81
|
-
as the generator will have created a migration file (if your ORM supports them).
|
82
|
-
|
83
|
-
## Example
|
104
|
+
Replace MODEL by the class name used for the applications settings, it's frequently `Settings` but it may also be `Configuration` or something else. This will create a model (if one does not exist) and configure it with default options.
|
84
105
|
|
106
|
+
Next, you'll usually run
|
85
107
|
```console
|
86
|
-
rails g settingson Settings
|
87
108
|
rake db:migrate
|
88
109
|
```
|
89
|
-
|
90
|
-
In rails console:
|
91
|
-
```ruby
|
92
|
-
2.1.1 :006 > Settings.hello.hello3.hello2
|
93
|
-
Settings Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello' LIMIT 1
|
94
|
-
Settings Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello.hello3' LIMIT 1
|
95
|
-
Settings Load (0.1ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello.hello3.hello2' LIMIT 1
|
96
|
-
=> #<Settingson::Store:0x000001016aee68 @klass=Settings(id: integer, name: string, value: text, created_at: datetime, updated_at: datetime), @name="hello.hello3.hello2", @value=#<Settingson::Store:0x000001016aee68 ...>>
|
97
|
-
|
98
|
-
2.1.1 :007 > Settings.hello.hello3.hello2 = 1
|
99
|
-
Settings Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello' LIMIT 1
|
100
|
-
Settings Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello.hello3' LIMIT 1
|
101
|
-
Settings Load (0.1ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello.hello3.hello2' LIMIT 1
|
102
|
-
(0.1ms) begin transaction
|
103
|
-
SQL (5.2ms) INSERT INTO "settings" ("created_at", "name", "updated_at", "value") VALUES (?, ?, ?, ?) [["created_at", Sat, 03 May 2014 09:45:25 UTC +00:00], ["name", "hello.hello3.hello2"], ["updated_at", Sat, 03 May 2014 09:45:25 UTC +00:00], ["value", "--- 1\n...\n"]]
|
104
|
-
(2.4ms) commit transaction
|
105
|
-
=> 1
|
106
|
-
|
107
|
-
2.1.1 :008 > Settings.hello.hello3.hello2
|
108
|
-
Settings Load (0.3ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello' LIMIT 1
|
109
|
-
Settings Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello.hello3' LIMIT 1
|
110
|
-
Settings Load (0.1ms) SELECT "settings".* FROM "settings" WHERE "settings"."name" = 'hello.hello3.hello2' LIMIT 1
|
111
|
-
=> 1
|
112
|
-
```
|
110
|
+
as the generator will have created a migration file (if your ORM supports them).
|
113
111
|
|
114
112
|
## Contributing
|
115
113
|
|
@@ -8,13 +8,23 @@ module Settingson::Base
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_s
|
11
|
-
self.new_record? ? '' :
|
11
|
+
self.new_record? ? '' : super
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
self.new_record? ? '""' : super
|
12
16
|
end
|
13
17
|
|
14
18
|
def to_i
|
15
|
-
self.new_record? ? 0 :
|
19
|
+
self.new_record? ? 0 : super
|
20
|
+
end
|
21
|
+
|
22
|
+
def nil?
|
23
|
+
self.new_record? ? true : super
|
16
24
|
end
|
17
25
|
|
26
|
+
alias empty? nil?
|
27
|
+
|
18
28
|
def method_missing(symbol, *args)
|
19
29
|
super
|
20
30
|
rescue NoMethodError
|
@@ -22,7 +32,11 @@ module Settingson::Base
|
|
22
32
|
case symbol.to_s
|
23
33
|
when /(.+)=/ # setter
|
24
34
|
|
25
|
-
@settingson
|
35
|
+
if not defined?(@settingson) or @settingson.blank?
|
36
|
+
@settingson = $1
|
37
|
+
else
|
38
|
+
@settingson += ".#{$1}"
|
39
|
+
end
|
26
40
|
|
27
41
|
if record = self.class.find_by(key: @settingson) and args.first.nil?
|
28
42
|
record.destroy
|
@@ -31,6 +45,7 @@ module Settingson::Base
|
|
31
45
|
else
|
32
46
|
self.class.create(key: @settingson, value: args.first)
|
33
47
|
end
|
48
|
+
|
34
49
|
when /(.+)\?$/ # returns boolean
|
35
50
|
@settingson = "#{@settingson}.#{$1}"
|
36
51
|
self.class.find_by(key: @settingson).present?
|
@@ -56,6 +71,15 @@ module Settingson::Base
|
|
56
71
|
|
57
72
|
module ClassMethods
|
58
73
|
|
74
|
+
def from_hash(attributes)
|
75
|
+
case attributes
|
76
|
+
when Hash
|
77
|
+
attributes.map{|k,v| find_or_create_by(key: k).update!(value: v)}
|
78
|
+
else
|
79
|
+
false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
59
83
|
def method_missing(symbol, *args)
|
60
84
|
super
|
61
85
|
rescue NoMethodError
|
@@ -72,8 +96,7 @@ module Settingson::Base
|
|
72
96
|
else
|
73
97
|
create(key: @settingson, value: args.first, settingson: @settingson)
|
74
98
|
end
|
75
|
-
|
76
|
-
when /(.+)\?$/ #
|
99
|
+
when /(.+)\?$/ # returns boolean
|
77
100
|
find_by(key: $1).present?
|
78
101
|
when /(.+)\!$/ # returns self or nil
|
79
102
|
find_by(key: $1)
|
@@ -91,4 +114,4 @@ module Settingson::Base
|
|
91
114
|
|
92
115
|
end
|
93
116
|
|
94
|
-
end
|
117
|
+
end
|
data/lib/settingson/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: settingson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|