rails_admin_settings 0.3.4 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,6 +31,7 @@ Add this line to your application's Gemfile:
31
31
  - Put it after validates_email_format_of to get built-in support
32
32
  - Put it after geocoder to get built-in support
33
33
  - Put it after carrierwave / paperclip to get built-in support
34
+ - Put it after addressable to get built-in support
34
35
 
35
36
  And then execute:
36
37
 
@@ -69,6 +70,9 @@ Supported types:
69
70
  email (requires validates_email_format_of)
70
71
  address (requires geocoder)
71
72
  file (requires paperclip or carrierwave)
73
+ url (requires addressable)
74
+ domain (requires addressable)
75
+
72
76
 
73
77
  Strings and html support following replacement patterns:
74
78
 
@@ -1,4 +1,4 @@
1
- - if ['string', 'integer', 'phone', 'email', 'address'].include?(@object.type)
1
+ - if ['string', 'integer', 'phone', 'email', 'address', 'url', 'domain'].include?(@object.type)
2
2
  = form.text_field :raw, :value => field.value
3
3
  - elsif @object.type == 'text' || @object.type == 'yaml'
4
4
  = form.text_area :raw, :value => field.value, :rows => 10, :cols => 80
@@ -1,7 +1,7 @@
1
1
  module RailsAdminSettings
2
2
  module Processing
3
3
  def text_type?
4
- ['string', 'html', 'sanitized', 'email', 'address'].include? type
4
+ ['string', 'html', 'sanitized', 'email', 'address', 'url', 'domain'].include? type
5
5
  end
6
6
 
7
7
  def html_type?
@@ -36,6 +36,14 @@ module RailsAdminSettings
36
36
  'file' == type
37
37
  end
38
38
 
39
+ def url_type?
40
+ 'url' == type
41
+ end
42
+
43
+ def domain_type?
44
+ 'domain' == type
45
+ end
46
+
39
47
  def value
40
48
  if file_type?
41
49
  file.url
@@ -51,6 +51,16 @@ module RailsAdminSettings
51
51
  raise e
52
52
  end
53
53
  end
54
+
55
+ def require_addressable
56
+ begin
57
+ require 'addressable/uri'
58
+ yield
59
+ rescue LoadError => e
60
+ e.message << " [rails_admin_settings] Please add gem 'addressable' to your Gemfile to use url/domain type settings"
61
+ raise e
62
+ end
63
+ end
54
64
  end
55
65
  end
56
66
 
@@ -53,7 +53,7 @@ class Settings
53
53
  end
54
54
 
55
55
  if @@settings[key].nil?
56
- @@settings[key] = RailsAdminSettings::Setting.create(options.merge(key: key, raw: value))
56
+ write_to_database(key, options.merge(key: key, raw: value))
57
57
  else
58
58
  @@settings[key].update_attributes!(options.merge(raw: value))
59
59
  @@settings[key]
@@ -97,19 +97,42 @@ class Settings
97
97
  def save_default(key, value, options = {})
98
98
  load!
99
99
  options.merge!(default: value)
100
- create_setting(key, options) if @@settings[key].nil?
100
+ if @@settings[key].nil?
101
+ create_setting(key, options)
102
+ else
103
+ @@settings[key]
104
+ end
101
105
  end
102
106
 
103
107
  def create_setting(key, options = {})
104
108
  load!
105
109
  options.symbolize_keys!
106
110
  options[:raw] = options.delete(:default)
107
- @@settings[key] = RailsAdminSettings::Setting.create!(options.merge(key: key)) if @@settings[key].nil?
111
+ if @@settings[key].nil?
112
+ write_to_database(key, options.merge(key: key))
113
+ else
114
+ @@settings[key]
115
+ end
108
116
  end
109
117
 
110
118
  # to satisfy rspec
111
119
  def to_ary
112
120
  ['Settings']
113
121
  end
122
+
123
+ def write_to_database(key, options)
124
+ begin
125
+ @@settings[key] = RailsAdminSettings::Setting.create!(options)
126
+ rescue Mongoid::Errors::Validations => e
127
+ if @@settings[key].errors[:key].any?
128
+ @@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)
134
+ end
135
+ end
136
+ end
114
137
  end
115
138
  end
@@ -1,5 +1,5 @@
1
1
  module RailsAdminSettings
2
2
  def self.types
3
- ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone', 'email', 'address', 'file']
3
+ ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone', 'email', 'address', 'file', 'url', 'domain']
4
4
  end
5
5
  end
@@ -33,6 +33,17 @@ module RailsAdminSettings
33
33
  end
34
34
  end
35
35
 
36
+ base.before_validation if: :url_type? do
37
+ require_addressable do
38
+ self.raw = Addressable::URI.heuristic_parse(self.raw) unless self.raw.blank?
39
+ end
40
+ end
41
+
42
+ base.before_validation if: :domain_type? do
43
+ require_addressable do
44
+ self.raw = Addressable::URI.heuristic_parse(self.raw).host unless self.raw.blank?
45
+ end
46
+ end
36
47
 
37
48
  if Object.const_defined?('Geocoder')
38
49
  base.field(:coordinates, type: Array, default: [])
@@ -1,3 +1,3 @@
1
1
  module RailsAdminSettings
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.6"
3
3
  end
@@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency "sanitize", "~> 2.0.3"
34
34
  spec.add_development_dependency "validates_email_format_of", "~> 1.5.3"
35
35
  spec.add_development_dependency "geocoder", "~> 1.1.6"
36
+ spec.add_development_dependency "addressable", "~> 2.3.3"
36
37
  end
@@ -63,4 +63,10 @@ describe 'Settings advanced usage' do
63
63
  Settings.eml = 'test@example.com'
64
64
  Settings.eml.should eq 'test@example.com'
65
65
  end
66
+
67
+ it 'should process urls with url type' do
68
+ Settings.url(type: 'url')
69
+ Settings.url = 'test.ru'
70
+ Settings.url.should eq 'http://test.ru'
71
+ end
66
72
  end
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.4
4
+ version: 0.3.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -219,6 +219,22 @@ dependencies:
219
219
  - - ~>
220
220
  - !ruby/object:Gem::Version
221
221
  version: 1.1.6
222
+ - !ruby/object:Gem::Dependency
223
+ name: addressable
224
+ requirement: !ruby/object:Gem::Requirement
225
+ none: false
226
+ requirements:
227
+ - - ~>
228
+ - !ruby/object:Gem::Version
229
+ version: 2.3.3
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ none: false
234
+ requirements:
235
+ - - ~>
236
+ - !ruby/object:Gem::Version
237
+ version: 2.3.3
222
238
  description: Mongoid / RailsAdmin App Settings management
223
239
  email:
224
240
  - glebtv@gmail.com
@@ -273,7 +289,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
273
289
  version: '0'
274
290
  segments:
275
291
  - 0
276
- hash: 126914792518638842
292
+ hash: 759672937259672322
277
293
  required_rubygems_version: !ruby/object:Gem::Requirement
278
294
  none: false
279
295
  requirements:
@@ -282,7 +298,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
282
298
  version: '0'
283
299
  segments:
284
300
  - 0
285
- hash: 126914792518638842
301
+ hash: 759672937259672322
286
302
  requirements: []
287
303
  rubyforge_project:
288
304
  rubygems_version: 1.8.25