rails-settings-manager 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: a70c010193fe02c5bf40d844c72d6e308e6b7d2a
4
- data.tar.gz: 5dbf55864c160367484428233f0ec45add65554b
3
+ metadata.gz: 83ed339be4d77461001dfbaefe1dc83517469dc7
4
+ data.tar.gz: 316a936f3f82a34833e5aae91ca5fb0dcbf579ab
5
5
  SHA512:
6
- metadata.gz: e2e1a5d2dc1af98ee279a555e8fb002d0efae686c4ea0a66a8f4ad4fa0d2487dcb064111359c69753717eb825d871e60d89cf1212512242e9fc0bf875364d61a
7
- data.tar.gz: 595aa8994392a0d1207a9b0ce3ee63c492f0889068e84054a8d24b284a549b3729125849f687b26a596553a5a1a733847768f66ce401bbdc3bfae03a9f9d79fe
6
+ metadata.gz: 8362af4ec739fae4b6adda5995384ab6fe9928b8a34a59146a050e4401873fac68ff67662d841e412d775e2c4626127f87233b24b79376155c7753f76a01d636
7
+ data.tar.gz: 35e183443e2d1997033f34e00afbd15ee2307450dbd6561fb91249957a0eebbe1131668904b8b7748d3d0797e7d88a2a2ae3e10344b689d1f2590587cd862c3f
data/README.md CHANGED
@@ -1,15 +1,26 @@
1
- # Rails::Settings::Manager
1
+ # rails-settings-manager
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails/settings/manager`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A simple extension-plugin for Ruby on Rails application for global settings management in the Database with an easy key-value interface. It keeps track of the settings with the help of standard ActiveRecord methods.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Features
6
+
7
+ * Simple management of a global settings table (or even multiple if wanted) in Ruby on Rails applications with an easy key-value interface
8
+ * Behaves like a global Hash stored in the database of the app with full-usage of standard ActiveRecord methods for manipulation, queries and validation
9
+ * Support for key limitations and setting-value validations in the setting models
10
+ * Support for any kind of Object (e.g. ` integer `, ` float `, ` string ` or ` array `)
11
+ * Also modular application structures (e.g. ` Rails::Engine `) are supported
12
+
13
+ ## Requirements
14
+
15
+ * Ruby (Version ` >= 2.1.0 `)
16
+ * Rails (Version ` >= 4.2.0 `) [Note: ` Rails V 5.0.0 ` and above should also work fine, but are not tested yet completly. If any failures might happen with it feel free to report an [Issue](https://github.com/fnitschmann/rails-settings-manager/issues)]
6
17
 
7
18
  ## Installation
8
19
 
9
20
  Add this line to your application's Gemfile:
10
21
 
11
22
  ```ruby
12
- gem 'rails-settings-manager'
23
+ gem "rails-settings-manager"
13
24
  ```
14
25
 
15
26
  And then execute:
@@ -19,23 +30,191 @@ And then execute:
19
30
  Or install it yourself as:
20
31
 
21
32
  $ gem install rails-settings-manager
33
+
34
+
35
+ Generate the required settings files:
36
+
37
+ $ rails g settings:install
38
+
39
+ If wanted with a custom model name:
40
+
41
+ $ rails g settings:install CustomName
42
+
43
+ Then run your migrations:
44
+
45
+ $ bundle exec rake db:migrate
22
46
 
23
47
  ## Usage
24
48
 
25
- TODO: Write usage instructions here
49
+ ### Basic syntax
26
50
 
27
- ## Development
51
+ The setting syntax is easy. Create some basic settings if wanted:
52
+
53
+ ```ruby
54
+ Setting.admin_username = "admin"
55
+ Setting.admin_password = "superadminpassword"
56
+ Setting.some_numbers = 123
57
+ Setting.other_credentials = { :username => "user", :password => "userpassword" }
58
+ Setting["array_like_setting"] = []
59
+ ```
60
+
61
+ Read them:
62
+
63
+ ```ruby
64
+ Setting.some_numbers # returns 123
65
+ Setting["some_numbers"] # returns 123
66
+ ```
67
+
68
+ Change already existing setting:
28
69
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+ ```ruby
71
+ Setting.some_numbers = 321 # returns 321
72
+ ```
73
+
74
+ Set multiple at once:
75
+
76
+ ```ruby
77
+ Setting.set(:foo => "bar", :bar => "baz")
78
+ ```
79
+
80
+ Destroy a single setting and read it again:
81
+
82
+ ```ruby
83
+ Setting.destroy!(:some_numbers) # will raise SettingsManager::Errors::SettingNotFoundError
84
+ # if key not set yet
85
+ Setting.some_numbers # returns nil or (if set) the default value
86
+ ```
87
+
88
+ Get all settings at once:
89
+
90
+ ```ruby
91
+ Setting.get_all # returns Hash
92
+ ```
93
+
94
+ ### Defaults
95
+
96
+ The Gem supports ` .yml ` files which can hold default settings.
97
+
98
+ To set the default to a settings model add the following lines:
99
+
100
+ ```ruby
101
+ class Setting < SettingsManager::Base
102
+ default_settings_config Rails.root.join("config/default_settings.yml")
103
+ end
104
+ ```
105
+
106
+ The specified file should look like:
107
+
108
+ ```yaml
109
+ defaults: &defaults
110
+ some_key: "some_value"
111
+
112
+ development:
113
+ <<: *defaults
114
+
115
+ test:
116
+ <<: *defaults
117
+
118
+ production:
119
+ <<: *defaults
120
+ ```
30
121
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
122
+ Test it:
123
+
124
+ ```ruby
125
+ Setting.some_key # returns "some_value" if nothing in the Database is present
126
+ ```
127
+
128
+ ### Key limitations
129
+
130
+ There is also an option to restrict to usage of certain keys.
131
+
132
+ ```ruby
133
+ class Setting < SettingsManager::Base
134
+ allowed_settings_keys [:admin_username, :admin_password]
135
+ ...
136
+ end
137
+ ```
138
+
139
+ Test it:
140
+
141
+ ```ruby
142
+ Setting.foo # will raise SettingsManager::Errors::KeyInvalidError
143
+ Setting.foo = "bar" # will raise SettingsManager::Errors::KeyInvalidError
144
+ Setting.admin_username = "xxx" # returns "xxx"
145
+ ```
146
+ Note: Check the [Errors Module](https://github.com/fnitschmann/rails-settings-manager/blob/master/lib/settings-manager/base.rb) for more details about the exceptions
147
+
148
+ ### Validation
149
+
150
+ Validations of the setting values for certain keys are also possible. You can use all the [ActiveRecord Validations](http://guides.rubyonrails.org/active_record_validations.html). The only exception currently is, that you can't use an ` if ` block statement in the validation ` options `.
151
+
152
+ Example:
153
+
154
+ ```ruby
155
+ class Setting < SettingsManager::Base
156
+ validates_setting :setting_to_validate,
157
+ :length => { :minimum => 5, :maximum => 100 }
158
+ ...
159
+ end
160
+ ```
161
+
162
+ Test it out:
163
+
164
+ ```ruby
165
+ Setting.setting_to_validate = "123" # will raise SettingsManager::Errors::InvalidError
166
+ ```
167
+ Note: Check the [Errors Module](https://github.com/fnitschmann/rails-settings-manager/blob/master/lib/settings-manager/base.rb) for more details about the exceptions
168
+
169
+ ### Extension for models
170
+
171
+ Settings can be bound on any already-existing ActiveRecord object (aka model).
172
+ Define this association like this:
173
+
174
+ ```ruby
175
+ class User < ActiveRecord::Base
176
+ include SettingsManager::Extension
177
+
178
+ # Note: the following line is optional if the name of your settings model is 'Setting'
179
+ # if not so it is obligatory (String or Class)
180
+ settings_base_class Setting
181
+ end
182
+ ```
183
+
184
+ Usage:
185
+
186
+ ```ruby
187
+ user = User.find(1)
188
+ user.settings.foo = "bar"
189
+ user.settings.foo # returns "bar"
190
+ user.settings.get_all # returns { "foo" => "bar" }
191
+ ```
192
+
193
+ Scopes:
194
+
195
+ ```ruby
196
+ User.with_settings
197
+ # => returns all users with any settings
198
+
199
+ User.with_settings_for(:key)
200
+ # => returns all users with settings for 'key'
201
+
202
+ User.without_settings
203
+ # => returns all users without any settings
204
+
205
+ User.without_settings_for(:key)
206
+ # => returns all users without settings for 'key'
207
+ ```
208
+
209
+ ## Development
210
+
211
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake rspec` to run the tests.
32
212
 
33
213
  ## Contributing
34
214
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails-settings-manager.
215
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fnitschmann/rails-settings-manager
36
216
 
37
217
 
38
218
  ## License
39
219
 
40
220
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -1,3 +1,3 @@
1
1
  module SettingsManager
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-settings-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Nitschmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-10 00:00:00.000000000 Z
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails