rails_admin_settings 0.1.0 → 0.2.0

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.
data/README.md CHANGED
@@ -12,6 +12,7 @@ Pull request with AR support will be welcome
12
12
  - Lazy loading - loads settings only if they are needed during request
13
13
  - Loads all settings at once and caches them for the duration of request
14
14
  - Supports lots of setting types - yaml, html with ckeditor, phone numbers etc
15
+ - Each setting can be enabled and disabled within rails_admin, if it's disabled it returns default value for type
15
16
 
16
17
  ## Installation
17
18
 
@@ -27,6 +28,7 @@ Add this line to your application's Gemfile:
27
28
  - Put it after russian_phone to get built-in support
28
29
  - Put it after sanitized to get built-in support
29
30
  - Put it after safe_yaml to get built-in support
31
+ - Put it after validates_email_format_of to get built-in support
30
32
 
31
33
  And then execute:
32
34
 
@@ -46,6 +48,7 @@ Or install it yourself as:
46
48
 
47
49
  Settings.content_block_1(mode: 'html', default: 'test')
48
50
  Settings.data(mode: 'yaml')
51
+ Settings.enabled?(:data, type: 'phone') # also creates setting if it doesn't exist
49
52
  Settings.data = [1, 2, 3]
50
53
 
51
54
  See more here: https://github.com/rs-pro/rails_admin_settings/blob/master/spec/advanced_usage_spec.rb
@@ -57,10 +60,12 @@ Supported types:
57
60
  string (input)
58
61
  text (textarea)
59
62
  html (supports Rich, glebtv-ckeditor, ckeditor, but does not require any of them)
63
+ sanitized (requires sanitize -- sanitizes HTML before saving to DB [Warning: uses RELAXED config!])
60
64
  integer (stored as string)
61
65
  yaml (requires safe_yaml)
62
66
  phone (requires russian_phone)
63
- sanitized (requires sanitize)
67
+ email (requires validates_email_format_of)
68
+
64
69
 
65
70
  Strings and html support following replacement patterns:
66
71
 
@@ -1,4 +1,4 @@
1
- - if @object.type == 'string' || @object.type == 'integer'
1
+ - if ['string', 'integer', 'phone', 'email', 'address'].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
@@ -4,6 +4,7 @@ ru:
4
4
  label: 'Настройки'
5
5
  no_ckeditor_detected: CKEditor не обнаружен — отображаю настройку как текстовое поле
6
6
  phone_invalid: Неверный телефонный номер
7
+ email_invalid: Неверный email
7
8
  yaml_invalid: Неверный синтаксис YAML
8
9
  mongoid:
9
10
  models:
@@ -1,7 +1,7 @@
1
1
  module RailsAdminSettings
2
2
  module Processing
3
3
  def text_type?
4
- ['string', 'html', 'sanitized'].include? type
4
+ ['string', 'html', 'sanitized', 'email'].include? type
5
5
  end
6
6
 
7
7
  def html_type?
@@ -16,10 +16,18 @@ module RailsAdminSettings
16
16
  'yaml' == type
17
17
  end
18
18
 
19
+ def email_type?
20
+ 'email' == type
21
+ end
22
+
19
23
  def phone_type?
20
24
  'phone' == type
21
25
  end
22
26
 
27
+ def address_type?
28
+ 'address' == type
29
+ end
30
+
23
31
  def sanitized_type?
24
32
  'sanitized' == type
25
33
  end
@@ -59,7 +67,7 @@ module RailsAdminSettings
59
67
  nil
60
68
  elsif phone_type?
61
69
  require_russian_phone do
62
- RussianPhone::Number.new('(000) 000-00-00')
70
+ RussianPhone::Number.new('')
63
71
  end
64
72
  else
65
73
  nil
@@ -1,7 +1,7 @@
1
1
  module RailsAdminSettings
2
2
  module RailsAdminConfig
3
3
  def self.included(base)
4
- rails_admin do
4
+ base.rails_admin do
5
5
  navigation_label t('admin.settings.label')
6
6
 
7
7
  object_label_method do
@@ -27,7 +27,27 @@ module RailsAdminSettings
27
27
  require 'sanitize'
28
28
  yield
29
29
  rescue LoadError => e
30
- e.message << " [rails_admin_settings] Please install sanitize to use sanitized settings"
30
+ e.message << " [rails_admin_settings] Please add gem 'sanitize' to your Gemfile to use sanitized settings"
31
+ raise e
32
+ end
33
+ end
34
+
35
+ def require_validates_email_format_of
36
+ begin
37
+ require 'validates_email_format_of'
38
+ yield
39
+ rescue LoadError => e
40
+ e.message << " [rails_admin_settings] Please add gem 'validates_email_format_of' to your Gemfile to use email type settings"
41
+ raise e
42
+ end
43
+ end
44
+
45
+ def require_geocoder
46
+ begin
47
+ require 'geocoder'
48
+ yield
49
+ rescue LoadError => e
50
+ e.message << " [rails_admin_settings] Please add gem 'geocoder' to your Gemfile to use address type settings"
31
51
  raise e
32
52
  end
33
53
  end
@@ -4,6 +4,8 @@ module RailsAdminSettings
4
4
  include ::Mongoid::Document
5
5
  include ::Mongoid::Timestamps::Short
6
6
 
7
+ store_in collection: "rails_admin_settings"
8
+
7
9
  if Object.const_defined?('Mongoid') && Mongoid.const_defined?('Audit')
8
10
  include ::Mongoid::Audit::Trackable
9
11
  track_history track_create: true, track_destroy: true
@@ -53,6 +53,10 @@ class Settings
53
53
  end
54
54
  end
55
55
 
56
+ def enabled?(key, options = {})
57
+ get(key, options).enabled?
58
+ end
59
+
56
60
  # returns setting object
57
61
  def get(key, options = {})
58
62
  load!
@@ -1,5 +1,5 @@
1
1
  module RailsAdminSettings
2
2
  def self.types
3
- ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone']
3
+ ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone', 'email']
4
4
  end
5
5
  end
@@ -11,16 +11,36 @@ module RailsAdminSettings
11
11
 
12
12
  base.validate if: :phone_type? do
13
13
  require_russian_phone do
14
- errors.add(:raw, t('admin.settings.phone_invalid')) if RussianPhone::Number.new(:raw).valid?
14
+ errors.add(:raw, I18n.t('admin.settings.phone_invalid')) unless raw.blank? || RussianPhone::Number.new(:raw).valid?
15
15
  end
16
16
  end
17
+
18
+ base.validate if: :email_type? do
19
+ require_validates_email_format_of do
20
+ errors.add(:raw, I18n.t('admin.settings.email_invalid')) unless raw.blank? || ValidatesEmailFormatOf.validate_email_format(raw).nil?
21
+ end
22
+ end
23
+
24
+ base.validate if: :address_type? do
25
+ require_geocoder do
26
+ # just raise error if we are trying to use address type without geocoder
27
+ end
28
+ end
29
+
30
+ if Object.const_defined?('Geocoder')
31
+ base.field(:coordinates, type: Array)
32
+ base.send(:include, Geocoder::Model::Mongoid)
33
+ base.geocoded_by(:raw)
34
+ base.after_validation(:geocode, if: :address_type?)
35
+ end
36
+
17
37
  base.validate if: :yaml_type? do
18
38
  require_safe_yaml do
19
39
  unless raw.blank?
20
40
  begin
21
41
  YAML.safe_load(raw)
22
42
  rescue Psych::SyntaxError => e
23
- errors.add(:raw, t('admin.settings.yaml_invalid'))
43
+ errors.add(:raw, I18n.t('admin.settings.yaml_invalid'))
24
44
  end
25
45
  end
26
46
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminSettings
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -48,10 +48,12 @@ describe 'Settings advanced usage' do
48
48
 
49
49
  Settings.get(:tphone).val.city.should eq '906'
50
50
  Settings.get(:tphone).val.formatted_subscriber.should eq '111-11-11'
51
+ end
51
52
 
53
+ it 'should set defaults for phone type' do
52
54
  Settings.dphone(type: 'phone')
53
55
 
54
- Settings.dphone.city.should eq '000'
55
- Settings.dphone.formatted_subscriber.should eq '000-00-00'
56
+ Settings.dphone.formatted_area.should eq ''
57
+ Settings.dphone.formatted_subscriber.should eq ''
56
58
  end
57
59
  end
@@ -35,4 +35,9 @@ describe Settings do
35
35
  YAML.safe_load(Settings.get(:tdata).raw).should eq ['one', 'two', 'three']
36
36
  Settings.tdata.should eq ['one', 'two', 'three']
37
37
  end
38
+
39
+ it '#enabled? sets defaults' do
40
+ Settings.enabled?(:phone, type: 'phone').should eq true
41
+ Settings.get(:phone).type.should eq 'phone'
42
+ end
38
43
  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.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -239,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  segments:
241
241
  - 0
242
- hash: -2035061184471093913
242
+ hash: 1852916835368037287
243
243
  required_rubygems_version: !ruby/object:Gem::Requirement
244
244
  none: false
245
245
  requirements:
@@ -248,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
248
248
  version: '0'
249
249
  segments:
250
250
  - 0
251
- hash: -2035061184471093913
251
+ hash: 1852916835368037287
252
252
  requirements: []
253
253
  rubyforge_project:
254
254
  rubygems_version: 1.8.25