rails_admin_settings 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -54,6 +54,16 @@ module RailsAdminSettings
54
54
  end
55
55
  end
56
56
 
57
+ def blank?
58
+ if file_type?
59
+ file.url.nil?
60
+ elsif raw.blank? || disabled?
61
+ true
62
+ else
63
+ false
64
+ end
65
+ end
66
+
57
67
  def to_s
58
68
  if yaml_type? || phone_type? || integer_type?
59
69
  raw
@@ -1,5 +1,5 @@
1
1
  class Settings
2
- cattr_accessor :file_uploads_supported, :file_uploads_engine
2
+ cattr_accessor :file_uploads_supported, :file_uploads_engine, :settings
3
3
  @@file_uploads_supported = false
4
4
  @@file_uploads_engine = false
5
5
 
@@ -32,6 +32,8 @@ class Settings
32
32
 
33
33
  # returns processed setting value
34
34
  def method_missing(key, *args)
35
+ key = key.to_s
36
+
35
37
  if key[-1] == '='
36
38
  key = key[0..-2]
37
39
  options = args[1] || {}
@@ -96,16 +98,22 @@ class Settings
96
98
 
97
99
  def save_default(key, value, options = {})
98
100
  load!
101
+ key = key.to_s
99
102
  options.merge!(default: value)
100
103
  if @@settings[key].nil?
101
- create_setting(key, options)
104
+ create_setting(key, options).val
102
105
  else
103
- @@settings[key]
106
+ if @@settings[key].blank?
107
+ set(key, value).val
108
+ else
109
+ @@settings[key].val
110
+ end
104
111
  end
105
112
  end
106
113
 
107
114
  def create_setting(key, options = {})
108
115
  load!
116
+ key = key.to_s
109
117
  options.symbolize_keys!
110
118
  options[:raw] = options.delete(:default)
111
119
  if @@settings[key].nil?
@@ -120,19 +128,45 @@ class Settings
120
128
  ['Settings']
121
129
  end
122
130
 
131
+ def destroy(key)
132
+ raise 'please call destroy! to delete setting'
133
+ end
134
+
135
+ def destroy_all
136
+ raise 'please call destroy_all! to delete all settings'
137
+ end
138
+
139
+ def destroy!(key)
140
+ load!
141
+ key = key.to_s
142
+ unless @@settings[key].nil?
143
+ @@settings[key].destroy
144
+ @@settings.delete(key)
145
+ end
146
+ end
147
+
148
+ def destroy_all!
149
+ RailsAdminSettings::Setting.destroy_all
150
+ unload!
151
+ end
152
+
153
+
123
154
  def write_to_database(key, options)
124
- begin
125
- @@settings[key] = RailsAdminSettings::Setting.create!(options)
126
- rescue Mongoid::Errors::Validations => e
155
+ key = key.to_s
156
+ @@settings[key] = RailsAdminSettings::Setting.create(options)
157
+ unless @@settings[key].persisted?
127
158
  if @@settings[key].errors[:key].any?
128
159
  @@settings[key] = RailsAdminSettings::Setting.where(key: key).first
129
- @@settings[key].update_attributes!(options)
130
- end
131
-
132
- if @@settings[key].nil?
133
- @@settings[key] = RailsAdminSettings::Setting.new(options)
160
+ if options[:raw].blank? && !@@settings[key].blank?
161
+ # do not update setting if it's not blank in DB and we want to make it blank
162
+ else
163
+ unless @@settings[key].update_attributes(options)
164
+ raise RailsAdminSettings::PersistenceException
165
+ end
166
+ end
134
167
  end
135
168
  end
169
+ @@settings[key]
136
170
  end
137
171
  end
138
172
  end
@@ -46,7 +46,7 @@ module RailsAdminSettings
46
46
  end
47
47
 
48
48
  if Object.const_defined?('Geocoder')
49
- base.field(:coordinates, type: Array, default: [])
49
+ base.field(:coordinates, type: Array)
50
50
  base.send(:include, Geocoder::Model::Mongoid)
51
51
  base.geocoded_by(:raw)
52
52
  base.after_validation(:geocode, if: :address_type?)
@@ -1,3 +1,3 @@
1
1
  module RailsAdminSettings
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
@@ -1,6 +1,9 @@
1
1
  require "rails_admin_settings/version"
2
2
 
3
3
  module RailsAdminSettings
4
+ class PersistenceException < Exception
5
+ end
6
+
4
7
  autoload :Processing, "rails_admin_settings/processing"
5
8
  autoload :Validation, "rails_admin_settings/validation"
6
9
  autoload :RequireHelpers, "rails_admin_settings/require_helpers"
@@ -10,7 +10,7 @@ describe 'Settings advanced usage' do
10
10
  end
11
11
 
12
12
  it 'support html mode' do
13
- Settings.get(:email, type: 'html').to_s.should eq 'test@example.com'
13
+ Settings.get(:email, type: 'html', default: 'test@example.com').to_s.should eq 'test@example.com'
14
14
  end
15
15
 
16
16
  it 'support integer mode' do
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ # this tests check how well rails_admin_settings handles settings disappearing from DB during execution
6
+ # real usage: app specs with database_cleaner enabled
7
+ describe Settings do
8
+
9
+ it "should handle settings disappearing from DB" do
10
+ email = "my@mail.ru"
11
+ email2 = "my2@mail.ru"
12
+ Settings.email = email
13
+ Settings.email.should == email
14
+ RailsAdminSettings::Setting.destroy_all
15
+ # settings are still cached
16
+ Settings.email.should == email
17
+
18
+ Settings.email = email2
19
+ Settings.email.should == email2
20
+ end
21
+
22
+ it "should handle settings appearing from DB" do
23
+ Settings.tst2.should == ''
24
+ RailsAdminSettings::Setting.create(key: 'tst', raw: 'tst')
25
+ # settings are still cached, but when we try to create a setting it sees updated value in DB
26
+ Settings.tst.should == 'tst'
27
+ end
28
+
29
+ it "should handle settings appearing in DB" do
30
+ RailsAdminSettings::Setting.create(key: 'tst', raw: 'tst')
31
+
32
+ Settings.tst = 'str'
33
+ Settings.tst.should == 'str'
34
+ end
35
+
36
+ it "#destroy_all!" do
37
+ Settings.tst = 'str'
38
+ expect { Settings.destroy_all }.to raise_exception
39
+ Settings.destroy_all!
40
+ Settings.tst.should == ''
41
+ end
42
+ it "#destroy!" do
43
+ Settings.tst = 'str'
44
+ Settings.tst2 = 'str2'
45
+ expect { Settings.destroy(:tst) }.to raise_exception
46
+ Settings.destroy!(:tst)
47
+ Settings.tst.should == ''
48
+ Settings.tst2.should == 'str2'
49
+ end
50
+ end
@@ -5,12 +5,16 @@ require 'spec_helper'
5
5
  describe Settings do
6
6
 
7
7
  it "should works as RailsSettings" do
8
+ Settings.destroy_all!
9
+
8
10
  email = "my@mail.ru"
9
11
  Settings.email = email
10
12
  Settings.email.should == email
11
13
  end
12
14
 
13
15
  it "should save default" do
16
+ Settings.destroy_all!
17
+
14
18
  email = "my@mail.ru"
15
19
  email2 = "my2@mail.ru"
16
20
  Settings.save_default(:email, email)
@@ -4,6 +4,7 @@ RSpec.configure do |config|
4
4
  end
5
5
  config.after :each do
6
6
  DatabaseCleaner.clean
7
+ Settings.destroy_all!
7
8
  end
8
9
  end
9
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-23 00:00:00.000000000 Z
12
+ date: 2013-03-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -267,6 +267,7 @@ files:
267
267
  - lib/rails_admin_settings/version.rb
268
268
  - rails_admin_settings.gemspec
269
269
  - spec/advanced_usage_spec.rb
270
+ - spec/database_trickery_spec.rb
270
271
  - spec/factories/setting.rb
271
272
  - spec/model_spec.rb
272
273
  - spec/settings_spec.rb
@@ -289,7 +290,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
289
290
  version: '0'
290
291
  segments:
291
292
  - 0
292
- hash: 759672937259672322
293
+ hash: -2837920607569123901
293
294
  required_rubygems_version: !ruby/object:Gem::Requirement
294
295
  none: false
295
296
  requirements:
@@ -298,7 +299,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
298
299
  version: '0'
299
300
  segments:
300
301
  - 0
301
- hash: 759672937259672322
302
+ hash: -2837920607569123901
302
303
  requirements: []
303
304
  rubyforge_project:
304
305
  rubygems_version: 1.8.25
@@ -307,6 +308,7 @@ specification_version: 3
307
308
  summary: Setting for Rails app with mongoid and RailsAdmin
308
309
  test_files:
309
310
  - spec/advanced_usage_spec.rb
311
+ - spec/database_trickery_spec.rb
310
312
  - spec/factories/setting.rb
311
313
  - spec/model_spec.rb
312
314
  - spec/settings_spec.rb