rails_admin_settings 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -65,6 +65,7 @@ Supported types:
65
65
 
66
66
  string (input)
67
67
  text (textarea)
68
+ color (uses built-in RailsAdmin color picker)
68
69
  html (supports Rich, glebtv-ckeditor, ckeditor, but does not require any of them)
69
70
  sanitized (requires sanitize -- sanitizes HTML before saving to DB [Warning: uses RELAXED config!])
70
71
  integer (stored as string)
@@ -1,5 +1,7 @@
1
1
  - if ['string', 'integer', 'phone', 'email', 'address', 'url', 'domain'].include?(@object.type)
2
2
  = form.text_field :raw, :value => field.value
3
+ - if @object.type == 'color'
4
+ = form.text_field :raw, :value => field.value, 'data-color' => true
3
5
  - elsif @object.type == 'text' || @object.type == 'yaml'
4
6
  = form.text_area :raw, :value => field.value, :rows => 10, :cols => 80
5
7
  - elsif @object.type == 'html'
@@ -6,6 +6,7 @@ ru:
6
6
  phone_invalid: Incorrect phone
7
7
  email_invalid: Incorrect email
8
8
  yaml_invalid: Incorrect YAML
9
+ color_invalid: Incorrect color
9
10
  mongoid:
10
11
  models:
11
12
  rails_admin_settings/setting: Settings
@@ -6,6 +6,7 @@ ru:
6
6
  phone_invalid: Неверный телефонный номер
7
7
  email_invalid: Неверный email
8
8
  yaml_invalid: Неверный синтаксис YAML
9
+ color_invalid: Неверный цвет
9
10
  mongoid:
10
11
  models:
11
12
  rails_admin_settings/setting: Настройки
@@ -0,0 +1,11 @@
1
+ module RailsAdminSettings
2
+ class HexColorValidator < ActiveModel::EachValidator
3
+ def validate_each(record, attribute, value)
4
+ record.errors[attribute] << (options[:message] || I18n.t('admin.settings.invalid_color')) unless value.blank? || self.class.matches?(value)
5
+ end
6
+ def self.matches?(value)
7
+ return false unless value
8
+ /^(?:[0-9a-f]{3})(?:[0-9a-f]{3})?$/i.match(value).nil? ? false : true
9
+ end
10
+ end
11
+ end
@@ -1,49 +1,19 @@
1
1
  module RailsAdminSettings
2
2
  module Processing
3
+ RailsAdminSettings.types.each do |dtype|
4
+ define_method "#{dtype}_type?" do
5
+ dtype == type
6
+ end
7
+ end
8
+
3
9
  def text_type?
4
- ['string', 'html', 'sanitized', 'email', 'address', 'url', 'domain'].include? type
10
+ (RailsAdminSettings.types - ['phone', 'integer', 'yaml']).include? type
5
11
  end
6
12
 
7
13
  def html_type?
8
14
  ['html', 'sanitized'].include? type
9
15
  end
10
16
 
11
- def integer_type?
12
- 'integer' == type
13
- end
14
-
15
- def yaml_type?
16
- 'yaml' == type
17
- end
18
-
19
- def email_type?
20
- 'email' == type
21
- end
22
-
23
- def phone_type?
24
- 'phone' == type
25
- end
26
-
27
- def address_type?
28
- 'address' == type
29
- end
30
-
31
- def sanitized_type?
32
- 'sanitized' == type
33
- end
34
-
35
- def file_type?
36
- 'file' == type
37
- end
38
-
39
- def url_type?
40
- 'url' == type
41
- end
42
-
43
- def domain_type?
44
- 'domain' == type
45
- end
46
-
47
17
  def value
48
18
  if file_type?
49
19
  file.url
@@ -1,5 +1,5 @@
1
1
  module RailsAdminSettings
2
2
  def self.types
3
- ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone', 'email', 'address', 'file', 'url', 'domain']
3
+ ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone', 'email', 'address', 'file', 'url', 'domain', 'color']
4
4
  end
5
5
  end
@@ -52,6 +52,10 @@ module RailsAdminSettings
52
52
  base.after_validation(:geocode, if: :address_type?)
53
53
  end
54
54
 
55
+ base.validate if: :color_type? do
56
+ base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw)
57
+ end
58
+
55
59
  base.validate if: :yaml_type? do
56
60
  require_safe_yaml do
57
61
  unless raw.blank?
@@ -1,3 +1,3 @@
1
1
  module RailsAdminSettings
2
- VERSION = "0.3.9"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -4,11 +4,12 @@ module RailsAdminSettings
4
4
  class PersistenceException < Exception
5
5
  end
6
6
 
7
- autoload :Processing, "rails_admin_settings/processing"
8
- autoload :Validation, "rails_admin_settings/validation"
9
- autoload :RequireHelpers, "rails_admin_settings/require_helpers"
10
- autoload :RailsAdminConfig, "rails_admin_settings/rails_admin_config"
11
- autoload :Uploads, "rails_admin_settings/uploads"
7
+ autoload :Processing, "rails_admin_settings/processing"
8
+ autoload :Validation, "rails_admin_settings/validation"
9
+ autoload :RequireHelpers, "rails_admin_settings/require_helpers"
10
+ autoload :RailsAdminConfig, "rails_admin_settings/rails_admin_config"
11
+ autoload :Uploads, "rails_admin_settings/uploads"
12
+ autoload :HexColorValidator, "rails_admin_settings/hex_color_validator"
12
13
  end
13
14
 
14
15
  require "rails_admin_settings/types"
@@ -8,65 +8,4 @@ describe 'Settings advanced usage' do
8
8
  s.should eq 'test@example.com'
9
9
  Settings.get(:email).to_s.should eq 'test@example.com'
10
10
  end
11
-
12
- it 'support html mode' do
13
- Settings.get(:email, type: 'html', default: 'test@example.com').to_s.should eq 'test@example.com'
14
- end
15
-
16
- it 'support integer mode' do
17
- Settings.get(:testint, type: 'integer').value.should eq 0
18
- Settings.get(:testint, default: 5, type: 'integer').value.should eq 0
19
- Settings.get(:testint2, default: 5, type: 'integer').value.should eq 5
20
- Settings.testint2.should eq 5
21
- end
22
-
23
- it 'support yaml mode' do
24
- Settings.get(:test_yml, type: 'yaml').value.should be_nil
25
- Settings.get(:test_yml_2, default: '[t1, t2, t3]', type: 'yaml').value.should eq ['t1', 't2', 't3']
26
- Settings.test_yml_2.should eq ['t1', 't2', 't3']
27
- end
28
-
29
- it 'should have sensible defaults' do
30
- s = Settings.get(:test_sett)
31
- s.should_not be_nil
32
- s.type.should eq 'string'
33
- s.raw.should eq ''
34
- s.value.should eq ''
35
- end
36
-
37
- it 'should support yaml type' do
38
- Settings.set(:data, '[one, two, three]', type: 'yaml')
39
- Settings.get(:data).raw.should eq '[one, two, three]'
40
- Settings.data.should eq ['one', 'two', 'three']
41
- end
42
-
43
- it 'should support phone type' do
44
- Settings.set(:tphone, '906 111 11 11', type: 'phone')
45
- Settings.get(:tphone).val.class.name.should eq 'RussianPhone::Number'
46
- Settings.tphone.class.name.should eq 'RussianPhone::Number'
47
- Settings.tphone.should eq '906 111 11 11'
48
-
49
- Settings.get(:tphone).val.city.should eq '906'
50
- Settings.get(:tphone).val.formatted_subscriber.should eq '111-11-11'
51
- end
52
-
53
- it 'should set defaults for phone type' do
54
- Settings.dphone(type: 'phone')
55
-
56
- Settings.dphone.formatted_area.should eq ''
57
- Settings.dphone.formatted_subscriber.should eq ''
58
- end
59
-
60
- it 'should validate emails with email type' do
61
- Settings.eml(type: 'email')
62
- expect { Settings.eml = '1' }.to raise_error
63
- Settings.eml = 'test@example.com'
64
- Settings.eml.should eq 'test@example.com'
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
72
11
  end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings advanced usage' do
6
+ it 'support html type' do
7
+ Settings.get(:email, type: 'html', default: 'test@example.com').to_s.should eq 'test@example.com'
8
+ end
9
+
10
+ it 'support integer type' do
11
+ Settings.get(:testint, type: 'integer').value.should eq 0
12
+ Settings.get(:testint, default: 5, type: 'integer').value.should eq 0
13
+ Settings.get(:testint2, default: 5, type: 'integer').value.should eq 5
14
+ Settings.testint2.should eq 5
15
+ end
16
+
17
+ it 'supports yaml type' do
18
+ Settings.set(:data, '[one, two, three]', type: 'yaml')
19
+ Settings.get(:data).raw.should eq '[one, two, three]'
20
+ Settings.data.should eq ['one', 'two', 'three']
21
+ end
22
+
23
+ it 'supports phone type' do
24
+ Settings.set(:tphone, '906 111 11 11', type: 'phone')
25
+ Settings.get(:tphone).val.class.name.should eq 'RussianPhone::Number'
26
+ Settings.tphone.class.name.should eq 'RussianPhone::Number'
27
+ Settings.tphone.should eq '906 111 11 11'
28
+ Settings.get(:tphone).phone_type?.should be_true
29
+ Settings.get(:tphone).val.city.should eq '906'
30
+ Settings.get(:tphone).val.formatted_subscriber.should eq '111-11-11'
31
+ end
32
+
33
+ it 'sets defaults for phone type' do
34
+ Settings.dphone(type: 'phone')
35
+ Settings.get(:dphone).phone_type?.should be_true
36
+ Settings.dphone.formatted_area.should eq ''
37
+ Settings.dphone.formatted_subscriber.should eq ''
38
+ end
39
+
40
+ it 'validates emails with email type' do
41
+ Settings.eml(type: 'email')
42
+ Settings.get(:eml).email_type?.should be_true
43
+ expect { Settings.eml = '1' }.to raise_error
44
+ Settings.eml = 'test@example.com'
45
+ Settings.eml.should eq 'test@example.com'
46
+ end
47
+
48
+ it 'processes urls with url type' do
49
+ Settings.url(type: 'url')
50
+ Settings.get(:url).url_type?.should be_true
51
+ Settings.get(:url).color_type?.should be_false
52
+ Settings.url = 'test.ru'
53
+ Settings.url.should eq 'http://test.ru'
54
+ end
55
+
56
+ it 'supports color type' do
57
+ Settings.col(type: 'color')
58
+ Settings.get(:col).color_type?.should be_true
59
+ expect { Settings.col = 'test'}.to raise_error
60
+ Settings.col = 'ffffff'
61
+ Settings.col.should eq 'ffffff'
62
+ end
63
+ 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.9
4
+ version: 0.4.0
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-25 00:00:00.000000000 Z
12
+ date: 2013-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -256,6 +256,7 @@ files:
256
256
  - config/locales/ru.rails_admin_settings.yml
257
257
  - lib/rails_admin_settings.rb
258
258
  - lib/rails_admin_settings/engine.rb
259
+ - lib/rails_admin_settings/hex_color_validator.rb
259
260
  - lib/rails_admin_settings/processing.rb
260
261
  - lib/rails_admin_settings/rails_admin_config.rb
261
262
  - lib/rails_admin_settings/require_helpers.rb
@@ -275,6 +276,7 @@ files:
275
276
  - spec/support/database_cleaner.rb
276
277
  - spec/support/mongoid.rb
277
278
  - spec/support/mongoid.yml
279
+ - spec/types_spec.rb
278
280
  homepage: https://github.com/rs-pro/rails_admin_settings
279
281
  licenses:
280
282
  - MIT
@@ -290,7 +292,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
290
292
  version: '0'
291
293
  segments:
292
294
  - 0
293
- hash: -2779903626978757826
295
+ hash: -3328257958509862173
294
296
  required_rubygems_version: !ruby/object:Gem::Requirement
295
297
  none: false
296
298
  requirements:
@@ -299,7 +301,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
299
301
  version: '0'
300
302
  segments:
301
303
  - 0
302
- hash: -2779903626978757826
304
+ hash: -3328257958509862173
303
305
  requirements: []
304
306
  rubyforge_project:
305
307
  rubygems_version: 1.8.25
@@ -316,3 +318,4 @@ test_files:
316
318
  - spec/support/database_cleaner.rb
317
319
  - spec/support/mongoid.rb
318
320
  - spec/support/mongoid.yml
321
+ - spec/types_spec.rb