real_settings 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
@@ -0,0 +1,18 @@
1
+ ### 0.0.1
2
+
3
+ * Initial release
4
+
5
+ ### 0.0.2
6
+
7
+ * Fix bugs
8
+
9
+ ### 0.0.3
10
+
11
+ * Fix bugs
12
+
13
+ ### 0.0.4
14
+
15
+ * Add 'db_config_default' config method
16
+ * change 'config' to 'file_config' for file config
17
+ * Add smart convert setting type
18
+ * Add column aut detect for simple_form
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # RealSettings
1
+ # RealSettings [![Build Status](https://secure.travis-ci.org/Macrow/real_settings.png)](http://travis-ci.org/Macrow/real_settings)
2
2
 
3
3
  RealSettings is a real settings tool for Rails3.
4
4
 
@@ -30,17 +30,23 @@ Run install command and migrate database:
30
30
 
31
31
  ## Configuration in config/initializers/real_settings.rb
32
32
 
33
+ Caution: settings in 'file_config' can't store in database.
34
+
33
35
  ```ruby
34
- Settings.config do
36
+ Settings.file_config do |settings|
35
37
  settings.app_name = "My Application Name"
36
38
  settings.app_url = "http://www.my-application.com"
37
39
  settings.default_meta_keywords = "default meta keywords"
38
40
  settings.default_meta_description = "default meta description"
39
41
  # add some settings here
40
42
  end
43
+
44
+ Settings.db_config_default do |settings|
45
+ settings.paginate_count = 10
46
+ end
41
47
  ```
42
48
 
43
- ## Caution: settings in config/initializers/real_settings.rb can't store in database.
49
+ ## Caution: settings set by 'file_config' in config/initializers/real_settings.rb can't store in database.
44
50
 
45
51
  ```ruby
46
52
  Settings.app_name # => "real_settings"
@@ -48,6 +54,11 @@ Settings.app_name = "new app name"
48
54
  Settings.app_name # => "new app name"
49
55
  Settings.save!
50
56
  Settings.app_name # => "real_settings"
57
+
58
+ Settings.paginate_count # => 10
59
+ Settings.paginate_count = 20
60
+ Settings.save!
61
+ Setttings.paginate_count # => 20
51
62
  ```
52
63
 
53
64
  ## Features & Usage
@@ -77,6 +88,27 @@ Settings.admin_name = "Macrow"
77
88
  Settings.save!
78
89
  ```
79
90
 
91
+ ### Smart Convert setting type
92
+
93
+ ```ruby
94
+ Settings.a = 123
95
+ Settings.b = 'string'
96
+ Settings.c = [1,2,3]
97
+ Settings.d = Time.now
98
+ Settings.e = 12.345
99
+ Settings.f = false
100
+
101
+ Settings.save!
102
+ Settings.reload!
103
+
104
+ Settings.a # 123 # Fixnum
105
+ Settings.b # 'string' # String
106
+ Settings.c # [1,2,3] # Array
107
+ Settings.d # Time.now # Time
108
+ Settings.e # 12.345 # Float
109
+ Settings.f # false # Boolean
110
+ ```
111
+
80
112
  ### Support has_settings method
81
113
 
82
114
  ```ruby
@@ -147,10 +179,16 @@ form_for Settings, :as => :settings, :url => settings_update_path, :method => :p
147
179
  ......
148
180
  end
149
181
 
182
+ simple_form_for Settings, :as => :settings, :url => settings_update_path, :method => :put do |f|
183
+ f.input :app_name
184
+ f.input :app_url
185
+ ......
186
+ end
187
+
150
188
  # Update Action:
151
189
  Settings.update_settings(params[:settings])
152
190
  ```
153
-
191
+
154
192
  ### Editing User's settings
155
193
 
156
194
  ```ruby
@@ -167,6 +205,12 @@ form_for @user.settings, :as => :settings, :url => user_settings_update_path(@us
167
205
  ......
168
206
  end
169
207
 
208
+ simple_form_for @user.settings, :as => :settings, :url => user_settings_update_path(@user), :method => :put do |f|
209
+ f.input :notebook
210
+ f.input :mobile
211
+ ......
212
+ end
213
+
170
214
  # Update Action:
171
215
  @user = User.find(params[:user_id])
172
216
  @user.settings.update_settings(params[:settings])
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ task :default => :rspec
5
+
6
+ task :rspec do
7
+ system 'rspec'
8
+ end
@@ -1,6 +1,10 @@
1
- # Caution: settings in config/initializers/real_settings.rb can't store in database.
1
+ # Caution: settings in 'file_config' can't store in database.
2
2
 
3
- Settings.config do |settings|
3
+ Settings.file_config do |settings|
4
4
  # settings.app_name = "My Application Name"
5
5
  # settings.app_url = "http://www.my-application.com"
6
+ end
7
+
8
+ Settings.db_config_default do |settings|
9
+ # settings.paginate_count = 10
6
10
  end
@@ -1,4 +1,6 @@
1
1
  require 'real_settings/version'
2
+ require 'real_settings/smart_convert'
3
+ require 'real_settings/fake_column'
2
4
  require 'real_settings/settings'
3
5
  require 'real_settings/owner_settings'
4
6
  require 'real_settings/relations'
@@ -0,0 +1,23 @@
1
+ # Hack for simple_form auto column type detectation
2
+ module RealSettings
3
+ class FakeColumn
4
+ attr_accessor :type, :number
5
+
6
+ def initialize(type, number = false)
7
+ self.type = type.downcase.to_sym
8
+ self.number = number
9
+ end
10
+
11
+ def number?
12
+ number
13
+ end
14
+
15
+ def limit
16
+ if type == 'string'
17
+ 255
18
+ else
19
+ nil
20
+ end
21
+ end
22
+ end
23
+ end
@@ -4,12 +4,23 @@ class Settings < ActiveRecord::Base
4
4
  cattr_accessor :__settings, :__file_settings, :__db_settings, :__temp_settings
5
5
 
6
6
  class << self
7
- def config
7
+ def file_config
8
8
  yield self
9
9
  self.__file_settings = __temp_settings
10
10
  reload!
11
11
  end
12
12
 
13
+ def db_config_default
14
+ yield self
15
+ db_default = __temp_settings
16
+ reload!
17
+ self.__settings = db_default.merge(__db_settings)
18
+ save!
19
+ end
20
+
21
+ # config method should be deprecated in future version
22
+ alias_method :config, :file_config
23
+
13
24
  def to_hash
14
25
  __settings.dup
15
26
  end
@@ -33,9 +44,9 @@ class Settings < ActiveRecord::Base
33
44
  __settings.delete_if { |key, value| value == __db_settings[key] || __hook_for_delete_owner_default_settings(key, value) }
34
45
  __settings.each do |key, value|
35
46
  if __db_settings.has_key?(key)
36
- where(:key => key).first.update_attribute(:value, value)
47
+ where(:key => key).first.update_attribute(:value, value.to_s)
37
48
  else
38
- create!(:key => key, :value => value, :target_id => target_id, :target_type => target_type)
49
+ create!(:key => key, :value => value.to_s, :target_id => target_id, :target_type => target_type)
39
50
  end
40
51
  end
41
52
  __db_settings.merge!(__settings)
@@ -56,20 +67,42 @@ class Settings < ActiveRecord::Base
56
67
  end
57
68
 
58
69
  def method_missing(name, *args)
59
- if self.respond_to?(name)
70
+ if respond_to?(name)
60
71
  super(name, args)
61
72
  else
62
73
  if name =~ /\w+=$/
63
74
  __temp_settings[name.to_s.downcase.gsub('=', '').to_sym] = args.first
64
75
  __settings[name.to_s.downcase.gsub('=', '').to_sym] = args.first
65
76
  elsif name =~ /\w+/
66
- __settings[name.to_s.downcase.to_sym] || __hook_for_load_owner_default_settings(name.to_s.downcase.to_sym)
77
+ if __settings[name.to_s.downcase.to_sym].nil?
78
+ __hook_for_load_owner_default_settings(name.to_s.downcase.to_sym)
79
+ else
80
+ __settings[name.to_s.downcase.to_sym]
81
+ end
67
82
  else
68
83
  raise NoMethodError
69
84
  end
70
85
  end
71
86
  end
72
87
 
88
+ # Hack for simple_form auto column type detectation
89
+ def column_for_attribute(name)
90
+ if __settings[name].nil?
91
+ RealSettings::FakeColumn.new('string')
92
+ else
93
+ case __settings[name].class.to_s
94
+ when 'Fixnum', 'Bignum'
95
+ RealSettings::FakeColumn.new('integer', true)
96
+ when 'Float'
97
+ RealSettings::FakeColumn.new('float', true)
98
+ when 'TrueClass', 'FalseClass'
99
+ RealSettings::FakeColumn.new('boolean')
100
+ else
101
+ RealSettings::FakeColumn.new('string')
102
+ end
103
+ end
104
+ end
105
+
73
106
  protected
74
107
 
75
108
  def __hook_for_load_owner_default_settings(key)
@@ -94,7 +127,7 @@ class Settings < ActiveRecord::Base
94
127
  def load_from_database!
95
128
  begin
96
129
  __db_settings = {}
97
- where(:target_type => target_type, :target_id => target_id).order(:key).all.each { |s| self.__db_settings[s.key.to_sym] = s.value }
130
+ where(:target_type => target_type, :target_id => target_id).order(:key).each { |s| self.__db_settings[s.key.to_sym] = RealSettings::SmartConvert.convert(s.value) }
98
131
  rescue
99
132
  __db_settings = {}
100
133
  end
@@ -0,0 +1,27 @@
1
+ require 'time'
2
+
3
+ module RealSettings
4
+ # Just convert simple format for : Time, Array(simple), Float, Fixnum, Boolean, String
5
+ class SmartConvert
6
+ def self.convert(value)
7
+ case value.strip
8
+ when /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \+\d{4}$/ # Time
9
+ Time.parse(value)
10
+ when /^\[.+\]$/ # Array
11
+ array = []
12
+ value.gsub(/\[|\]/,'').split(',').each {|v| array << RealSettings::SmartConvert.convert(v)}
13
+ array
14
+ when /^\d+\.\d+$/ # Float
15
+ value.to_f
16
+ when /^\d+$/ # Fixnum
17
+ value.to_i
18
+ when 'true'
19
+ true
20
+ when 'false'
21
+ false
22
+ else # String and Others
23
+ value
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module RealSettings
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["Macrow_wh@163.com"]
7
7
  gem.description = %q{A real settings tool for Rails3}
8
8
  gem.summary = %q{A real settings tool for Rails3}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/Macrow/real_settings"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -14,4 +14,9 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "real_settings"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = RealSettings::VERSION
17
+
18
+ gem.add_dependency 'rails'
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'sqlite3'
17
22
  end
@@ -6,22 +6,74 @@ describe "RealSettings" do
6
6
  before(:each) do
7
7
  @app_name = "real_settings"
8
8
  @app_url = "http://github.real_settings.com"
9
- Settings.config do |settings|
9
+ @app_online_at = Time.now
10
+ @paginate_count = 10
11
+ @app_open = true
12
+ Settings.file_config do |settings|
10
13
  settings.app_name = @app_name
11
14
  settings.app_url = @app_url
15
+ settings.app_online_at = @app_online_at
16
+ end
17
+ Settings.db_config_default do |settings|
18
+ settings.paginate_count = @paginate_count
19
+ settings.app_open = @app_open
12
20
  end
13
21
  @user1 = User.create!(:name => 'admin1')
14
22
  @user2 = User.create!(:name => 'admin2')
15
23
  end
16
24
 
25
+ it "save value and get right value type" do
26
+ at = Time.now
27
+ Settings.a_array = [1,2,3,4,5]
28
+ Settings.a_string = 'abc'
29
+ Settings.a_time = at
30
+ Settings.a_number = 123456
31
+ Settings.a_float = 123.456
32
+ Settings.a_bool_true = true
33
+ Settings.a_bool_false = false
34
+
35
+ Settings.save!
36
+ Settings.reload!
37
+
38
+ Settings.a_array.should == [1,2,3,4,5]
39
+ Settings.a_string.should == 'abc'
40
+ Settings.a_time.should.eql? at
41
+ Settings.a_number.should == 123456
42
+ Settings.a_float.should == 123.456
43
+ Settings.a_bool_true.should == true
44
+ Settings.a_bool_false.should == false
45
+ end
46
+
47
+ it "db_config_default load for default only" do
48
+ Settings.save!
49
+ Settings.reload!
50
+
51
+ Settings.paginate_count.should == @paginate_count
52
+ Settings.app_open.should == @app_open
53
+
54
+ Settings.db_config_default do |settings|
55
+ settings.paginate_count = 50
56
+ settings.app_open = false
57
+ end
58
+
59
+ Settings.save!
60
+ Settings.reload!
61
+
62
+ Settings.paginate_count.should == @paginate_count
63
+ Settings.app_open.should == @app_open
64
+ end
65
+
17
66
  it "load default settings from config file" do
18
67
  Settings.app_name.should == @app_name
19
68
  Settings.app_url.should == @app_url
69
+ Settings.app_online_at.should == @app_online_at
70
+ Settings.paginate_count.should == @paginate_count
71
+ Settings.app_open.should == @app_open
20
72
  end
21
73
 
22
74
  it "load settings priority is file > database > temp" do
23
- Settings.create(:key => 'app_name', :value => 'name from database')
24
- Settings.create(:key => 'app_url', :value => 'url from database')
75
+ Settings.app_name = 'name from database'
76
+ Settings.app_url = 'url from database'
25
77
  Settings.reload!
26
78
  Settings.app_name.should == @app_name
27
79
  Settings.app_url.should == @app_url
@@ -32,9 +84,26 @@ describe "RealSettings" do
32
84
  Settings.app_name.should == @app_name
33
85
  end
34
86
 
87
+ it "load db default config" do
88
+ Settings.paginate_count.should == @paginate_count
89
+ Settings.app_open.should == @app_open
90
+ Settings.reload!
91
+ Settings.paginate_count.should == @paginate_count
92
+ Settings.app_open.should.eql? @app_open
93
+ Settings.paginate_count = 30
94
+ new_open_time = Time.now
95
+ Settings.app_open = new_open_time
96
+ Settings.paginate_count.should == 30
97
+ Settings.app_open.should == new_open_time
98
+ Settings.save!
99
+ Settings.paginate_count.should == 30
100
+ Settings.app_open.should == new_open_time
101
+ end
102
+
35
103
  it "load default settings from database" do
36
- Settings.create(:key => 'another_name', :value => 'name from database')
37
- Settings.create(:key => 'another_url', :value => 'url from database')
104
+ Settings.another_name = 'name from database'
105
+ Settings.another_url = 'url from database'
106
+ Settings.save!
38
107
  Settings.reload!
39
108
  Settings.another_name.should == 'name from database'
40
109
  Settings.another_url.should == 'url from database'
@@ -133,30 +202,40 @@ describe "RealSettings" do
133
202
  end
134
203
 
135
204
  it "user has default settings" do
136
- # has_settings :defaults => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4' }
205
+ # has_settings :defaults => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4', :page_count => 20 }
137
206
  @user1.settings.to_hash[:notebook].should == 'Macbook Pro'
138
207
  @user1.settings.to_hash[:mobile].should == 'iPhone 4'
208
+ @user1.settings.to_hash[:page_count].should == 20
139
209
  @user1.settings.notebook.should == 'Macbook Pro'
140
210
  @user1.settings.mobile.should == 'iPhone 4'
211
+ @user1.settings.page_count.should == 20
212
+
141
213
  @user1.settings.notebook = 'Macbook Air'
142
214
  @user1.settings.mobile = 'iPhone 4S'
215
+ @user1.settings.page_count = 10
143
216
  @user1.settings.notebook.should == 'Macbook Air'
144
217
  @user1.settings.mobile.should == 'iPhone 4S'
218
+ @user1.settings.page_count.should == 10
145
219
  @user1.settings.reload!
146
220
  @user1.settings.notebook.should == 'Macbook Pro'
147
221
  @user1.settings.mobile.should == 'iPhone 4'
222
+ @user1.settings.page_count.should == 20
148
223
 
149
224
  @user1.settings.notebook = 'Macbook Air'
150
225
  @user1.settings.mobile = 'iPhone 4S'
226
+ @user1.settings.page_count = 10
151
227
  @user1.save!
152
228
  @user1.settings.notebook.should == 'Macbook Air'
153
229
  @user1.settings.mobile.should == 'iPhone 4S'
230
+ @user1.settings.page_count.should == 10
154
231
  @user1.settings.reload!
155
232
  @user1.settings.notebook.should == 'Macbook Air'
156
233
  @user1.settings.mobile.should == 'iPhone 4S'
234
+ @user1.settings.page_count.should == 10
157
235
 
158
236
  @user2.settings.notebook.should == 'Macbook Pro'
159
237
  @user2.settings.mobile.should == 'iPhone 4'
238
+ @user2.settings.page_count.should == 20
160
239
  end
161
240
 
162
241
  it "be sure destroy settings after destroy user" do
@@ -7,7 +7,7 @@ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":me
7
7
  ActiveRecord::Migration.verbose = false
8
8
 
9
9
  class User < ActiveRecord::Base
10
- has_settings :defaults => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4' }
10
+ has_settings :defaults => { :notebook => 'Macbook Pro', :mobile => 'iPhone 4', :page_count => 20 }
11
11
  end
12
12
 
13
13
  class Account < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: real_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,72 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-27 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-03-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
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
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sqlite3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
14
78
  description: A real settings tool for Rails3
15
79
  email:
16
80
  - Macrow_wh@163.com
@@ -19,6 +83,8 @@ extensions: []
19
83
  extra_rdoc_files: []
20
84
  files:
21
85
  - .gitignore
86
+ - .rspec
87
+ - CHANGELOG.md
22
88
  - Gemfile
23
89
  - LICENSE
24
90
  - README.md
@@ -30,14 +96,16 @@ files:
30
96
  - lib/generators/real_settings/install/templates/migration.rb
31
97
  - lib/generators/real_settings/install/templates/real_settings.rb
32
98
  - lib/real_settings.rb
99
+ - lib/real_settings/fake_column.rb
33
100
  - lib/real_settings/owner_settings.rb
34
101
  - lib/real_settings/relations.rb
35
102
  - lib/real_settings/settings.rb
103
+ - lib/real_settings/smart_convert.rb
36
104
  - lib/real_settings/version.rb
37
105
  - real_settings.gemspec
38
106
  - spec/real_settings_spec.rb
39
107
  - spec/spec_helper.rb
40
- homepage: ''
108
+ homepage: https://github.com/Macrow/real_settings
41
109
  licenses: []
42
110
  post_install_message:
43
111
  rdoc_options: []
@@ -51,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
119
  version: '0'
52
120
  segments:
53
121
  - 0
54
- hash: -3229865968002796882
122
+ hash: 77739245330383594
55
123
  required_rubygems_version: !ruby/object:Gem::Requirement
56
124
  none: false
57
125
  requirements:
@@ -60,10 +128,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
128
  version: '0'
61
129
  segments:
62
130
  - 0
63
- hash: -3229865968002796882
131
+ hash: 77739245330383594
64
132
  requirements: []
65
133
  rubyforge_project:
66
- rubygems_version: 1.8.24
134
+ rubygems_version: 1.8.25
67
135
  signing_key:
68
136
  specification_version: 3
69
137
  summary: A real settings tool for Rails3