rails_admin_settings 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,176 +1,176 @@
1
- class Settings
2
- cattr_accessor :file_uploads_supported, :file_uploads_engine, :settings
3
- @@file_uploads_supported = false
4
- @@file_uploads_engine = false
5
-
6
- class << self
7
- @@loaded = false
8
- @@settings = {}
9
-
10
- def load!
11
- if @@loaded
12
- false
13
- else
14
- @@settings = {}
15
- RailsAdminSettings::Setting.all.each do |setting|
16
- @@settings[setting.key] = setting
17
- end
18
- @@loaded = true
19
- true
20
- end
21
- end
22
-
23
- def unload!
24
- if @@loaded
25
- @@settings = {}
26
- @@loaded = false
27
- true
28
- else
29
- false
30
- end
31
- end
32
-
33
- # returns processed setting value
34
- def method_missing(key, *args)
35
- key = key.to_s
36
-
37
- if key[-1] == '='
38
- key = key[0..-2]
39
- options = args[1] || {}
40
- value = args.first
41
- set(key, value, options).val
42
- else
43
- get(key, args.first || {}).val
44
- end
45
- end
46
-
47
- def set(key, value = nil, options = {})
48
- load!
49
- key = key.to_s
50
- options.symbolize_keys!
51
-
52
-
53
- if !options[:type].nil? && options[:type] == 'yaml' && !value.nil? && !valid_yaml?(value)
54
- value = value.to_yaml
55
- end
56
-
57
- if @@settings[key].nil?
58
- write_to_database(key, options.merge(key: key, raw: value))
59
- else
60
- @@settings[key].update_attributes!(options.merge(raw: value))
61
- @@settings[key]
62
- end
63
- end
64
-
65
- def valid_yaml?(value)
66
- begin
67
- YAML.safe_load(value)
68
- rescue LoadError => e
69
- e.message << " [rails_admin_settings] Please add gem 'safe_yaml' to your Gemfile to use yaml settings"
70
- raise e
71
- rescue Psych::SyntaxError => e
72
- return false
73
- end
74
- end
75
-
76
- def enabled?(key, options = {})
77
- get(key, options).enabled?
78
- end
79
-
80
- # returns setting object
81
- def get(key, options = {})
82
- load!
83
- key = key.to_s
84
- if @@settings[key].nil?
85
- create_setting(key, options)
86
- else
87
- @@settings[key]
88
- end
89
- end
90
-
91
- def []=(key, value)
92
- set(key, value)
93
- end
94
-
95
- def [](key)
96
- get(key)
97
- end
98
-
99
- def save_default(key, value, options = {})
100
- load!
101
- key = key.to_s
102
- options.merge!(default: value)
103
- if @@settings[key].nil?
104
- create_setting(key, options).val
105
- else
106
- if @@settings[key].blank?
107
- set(key, value).val
108
- else
109
- @@settings[key].val
110
- end
111
- end
112
- end
113
-
114
- def create_setting(key, options = {})
115
- load!
116
- key = key.to_s
117
- options.symbolize_keys!
118
- options[:raw] = options.delete(:default)
119
- if @@settings[key].nil?
120
- write_to_database(key, options.merge(key: key))
121
- else
122
- @@settings[key]
123
- end
124
- end
125
-
126
- # to satisfy rspec
127
- def to_ary
128
- ['Settings']
129
- end
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
-
154
- def write_to_database(key, options)
155
- key = key.to_s
156
- @@settings[key] = RailsAdminSettings::Setting.create(options)
157
- unless @@settings[key].persisted?
158
- if @@settings[key].errors[:key].any?
159
- @@settings[key] = RailsAdminSettings::Setting.where(key: key).first
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
167
- end
168
- end
169
- @@settings[key]
170
- end
171
-
172
- def label key
173
- get(key).label
174
- end
175
- end
176
- end
1
+ class Settings
2
+ cattr_accessor :file_uploads_supported, :file_uploads_engine, :settings
3
+ @@file_uploads_supported = false
4
+ @@file_uploads_engine = false
5
+
6
+ class << self
7
+ @@loaded = false
8
+ @@settings = {}
9
+
10
+ def load!
11
+ if @@loaded
12
+ false
13
+ else
14
+ @@settings = {}
15
+ RailsAdminSettings::Setting.all.each do |setting|
16
+ @@settings[setting.key] = setting
17
+ end
18
+ @@loaded = true
19
+ true
20
+ end
21
+ end
22
+
23
+ def unload!
24
+ if @@loaded
25
+ @@settings = {}
26
+ @@loaded = false
27
+ true
28
+ else
29
+ false
30
+ end
31
+ end
32
+
33
+ # returns processed setting value
34
+ def method_missing(key, *args)
35
+ key = key.to_s
36
+
37
+ if key[-1] == '='
38
+ key = key[0..-2]
39
+ options = args[1] || {}
40
+ value = args.first
41
+ set(key, value, options).val
42
+ else
43
+ get(key, args.first || {}).val
44
+ end
45
+ end
46
+
47
+ def set(key, value = nil, options = {})
48
+ load!
49
+ key = key.to_s
50
+ options.symbolize_keys!
51
+
52
+
53
+ if !options[:type].nil? && options[:type] == 'yaml' && !value.nil? && !valid_yaml?(value)
54
+ value = value.to_yaml
55
+ end
56
+
57
+ if @@settings[key].nil?
58
+ write_to_database(key, options.merge(key: key, raw: value))
59
+ else
60
+ @@settings[key].update_attributes!(options.merge(raw: value))
61
+ @@settings[key]
62
+ end
63
+ end
64
+
65
+ def valid_yaml?(value)
66
+ begin
67
+ YAML.safe_load(value)
68
+ rescue LoadError => e
69
+ e.message << " [rails_admin_settings] Please add gem 'safe_yaml' to your Gemfile to use yaml settings"
70
+ raise e
71
+ rescue Psych::SyntaxError => e
72
+ return false
73
+ end
74
+ end
75
+
76
+ def enabled?(key, options = {})
77
+ get(key, options).enabled?
78
+ end
79
+
80
+ # returns setting object
81
+ def get(key, options = {})
82
+ load!
83
+ key = key.to_s
84
+ if @@settings[key].nil?
85
+ create_setting(key, options)
86
+ else
87
+ @@settings[key]
88
+ end
89
+ end
90
+
91
+ def []=(key, value)
92
+ set(key, value)
93
+ end
94
+
95
+ def [](key)
96
+ get(key)
97
+ end
98
+
99
+ def save_default(key, value, options = {})
100
+ load!
101
+ key = key.to_s
102
+ options.merge!(default: value)
103
+ if @@settings[key].nil?
104
+ create_setting(key, options).val
105
+ else
106
+ if @@settings[key].blank?
107
+ set(key, value).val
108
+ else
109
+ @@settings[key].val
110
+ end
111
+ end
112
+ end
113
+
114
+ def create_setting(key, options = {})
115
+ load!
116
+ key = key.to_s
117
+ options.symbolize_keys!
118
+ options[:raw] = options.delete(:default)
119
+ if @@settings[key].nil?
120
+ write_to_database(key, options.merge(key: key))
121
+ else
122
+ @@settings[key]
123
+ end
124
+ end
125
+
126
+ # to satisfy rspec
127
+ def to_ary
128
+ ['Settings']
129
+ end
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
+
154
+ def write_to_database(key, options)
155
+ key = key.to_s
156
+ @@settings[key] = RailsAdminSettings::Setting.create(options)
157
+ unless @@settings[key].persisted?
158
+ if @@settings[key].errors[:key].any?
159
+ @@settings[key] = RailsAdminSettings::Setting.where(key: key).first
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
167
+ end
168
+ end
169
+ @@settings[key]
170
+ end
171
+
172
+ def label key
173
+ get(key).label
174
+ end
175
+ end
176
+ end
@@ -1,9 +1,9 @@
1
- module RailsAdminSettings
2
- module Uploads
3
- class CarrierWave < CarrierWave::Uploader::Base
4
- def extension_white_list
5
- %w(jpg jpeg gif png tiff psd ai txt rtf doc docx xls xlsx ppt pptx odt odx zip rar 7z pdf)
6
- end
7
- end
8
- end
1
+ module RailsAdminSettings
2
+ module Uploads
3
+ class CarrierWave < CarrierWave::Uploader::Base
4
+ def extension_white_list
5
+ %w(jpg jpeg gif png tiff psd ai txt rtf doc docx xls xlsx ppt pptx odt odx zip rar 7z pdf)
6
+ end
7
+ end
8
+ end
9
9
  end
@@ -1,5 +1,5 @@
1
- module RailsAdminSettings
2
- def self.types
3
- ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone', 'email', 'address', 'file', 'url', 'domain', 'color']
4
- end
1
+ module RailsAdminSettings
2
+ def self.types
3
+ ['string', 'integer', 'html', 'sanitized', 'yaml', 'phone', 'email', 'address', 'file', 'url', 'domain', 'color']
4
+ end
5
5
  end
@@ -1,29 +1,30 @@
1
- module RailsAdminSettings
2
- module Uploads
3
- autoload :CarrierWave, "rails_admin_settings/storage/carrierwave"
4
-
5
- def self.included(base)
6
- # carrierwave
7
- if base.respond_to?(:mount_uploader)
8
- # puts "[rails_admin_settings] CarrierWave detected"
9
- base.field(:file, type: String)
10
- base.mount_uploader(:file, RailsAdminSettings::Uploads::CarrierWave)
11
- Settings.file_uploads_supported = true
12
- Settings.file_uploads_engine = :carrierwave
13
- # paperclip
14
- elsif Mongoid.const_defined?('Paperclip')
15
- base.send(:include, Mongoid::Paperclip)
16
- # puts "[rails_admin_settings] PaperClip detected"
17
- base.field(:file, type: String)
18
- base.has_mongoid_attached_file(:file)
19
- base.send(:attr_accessor, :delete_file)
20
- base.before_validation { self.file.clear if self.delete_file == '1' }
21
-
22
- Settings.file_uploads_supported = true
23
- Settings.file_uploads_engine = :paperclip
24
- else
25
- # puts "[rails_admin_settings] Uploads disabled"
26
- end
27
- end
28
- end
29
- end
1
+ module RailsAdminSettings
2
+ module Uploads
3
+ autoload :CarrierWave, "rails_admin_settings/storage/carrierwave"
4
+
5
+ def self.included(base)
6
+ # carrierwave
7
+ if base.respond_to?(:mount_uploader)
8
+ # puts "[rails_admin_settings] CarrierWave detected"
9
+ # base.field(:file, type: String)
10
+ base.mount_uploader(:file, RailsAdminSettings::Uploads::CarrierWave)
11
+ Settings.file_uploads_supported = true
12
+ Settings.file_uploads_engine = :carrierwave
13
+ # paperclip
14
+ elsif Mongoid.const_defined?('Paperclip')
15
+ base.send(:include, Mongoid::Paperclip)
16
+ # puts "[rails_admin_settings] PaperClip detected"
17
+ base.field(:file, type: String)
18
+ base.has_mongoid_attached_file(:file)
19
+ base.send(:attr_accessor, :delete_file)
20
+ base.before_validation { self.file.clear if self.delete_file == '1' }
21
+
22
+ Settings.file_uploads_supported = true
23
+ Settings.file_uploads_engine = :paperclip
24
+ else
25
+ # puts "[rails_admin_settings] Uploads disabled"
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -1,72 +1,72 @@
1
- module RailsAdminSettings
2
- module Validation
3
- def self.included(base)
4
- base.before_validation do
5
- self.raw = default_value if raw.blank?
6
- end
7
- base.before_validation :sanitize_value, if: :sanitized_type?
8
- base.validates_uniqueness_of :key
9
- base.validates_inclusion_of :type, in: RailsAdminSettings.types
10
- base.validates_numericality_of :raw, if: :integer_type?
11
-
12
- base.validate if: :phone_type? do
13
- require_russian_phone do
14
- errors.add(:raw, I18n.t('admin.settings.phone_invalid')) unless raw.blank? || RussianPhone::Number.new(raw).valid?
15
- end
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
- base.validate if: :file_type? do
31
- unless Settings.file_uploads_supported
32
- raise '[rails_admin_settings] File type requires either CarrierWave or Paperclip. Check that rails_admin_settings is below them in Gemfile'
33
- end
34
- end
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
47
-
48
- if Object.const_defined?('Geocoder')
49
- base.field(:coordinates, type: Array)
50
- base.send(:include, Geocoder::Model::Mongoid)
51
- base.geocoded_by(:raw)
52
- base.after_validation(:geocode, if: :address_type?)
53
- end
54
-
55
- base.validate if: :color_type? do
56
- base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw)
57
- end
58
-
59
- base.validate if: :yaml_type? do
60
- require_safe_yaml do
61
- unless raw.blank?
62
- begin
63
- YAML.safe_load(raw)
64
- rescue Psych::SyntaxError => e
65
- errors.add(:raw, I18n.t('admin.settings.yaml_invalid'))
66
- end
67
- end
68
- end
69
- end
70
- end
71
- end
72
- end
1
+ module RailsAdminSettings
2
+ module Validation
3
+ def self.included(base)
4
+ base.before_validation do
5
+ self.raw = default_value if raw.blank?
6
+ end
7
+ base.before_validation :sanitize_value, if: :sanitized_type?
8
+ base.validates_uniqueness_of :key
9
+ base.validates_inclusion_of :type, in: RailsAdminSettings.types
10
+ base.validates_numericality_of :raw, if: :integer_type?
11
+
12
+ base.validate if: :phone_type? do
13
+ require_russian_phone do
14
+ errors.add(:raw, I18n.t('admin.settings.phone_invalid')) unless raw.blank? || RussianPhone::Number.new(raw).valid?
15
+ end
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
+ base.validate if: :file_type? do
31
+ unless Settings.file_uploads_supported
32
+ raise '[rails_admin_settings] File type requires either CarrierWave or Paperclip. Check that rails_admin_settings is below them in Gemfile'
33
+ end
34
+ end
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
47
+
48
+ if Object.const_defined?('Geocoder')
49
+ base.field(:coordinates, type: Array)
50
+ base.send(:include, Geocoder::Model::Mongoid)
51
+ base.geocoded_by(:raw)
52
+ base.after_validation(:geocode, if: :address_type?)
53
+ end
54
+
55
+ base.validate if: :color_type? do
56
+ base.validates_with(RailsAdminSettings::HexColorValidator, attributes: :raw)
57
+ end
58
+
59
+ base.validate if: :yaml_type? do
60
+ require_safe_yaml do
61
+ unless raw.blank?
62
+ begin
63
+ YAML.safe_load(raw)
64
+ rescue Psych::SyntaxError => e
65
+ errors.add(:raw, I18n.t('admin.settings.yaml_invalid'))
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminSettings
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -1,11 +1,11 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe 'Settings advanced usage' do
6
- it 'with defaults' do
7
- s = Settings.email(default: 'test@example.com')
8
- s.should eq 'test@example.com'
9
- Settings.get(:email).to_s.should eq 'test@example.com'
10
- end
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Settings advanced usage' do
6
+ it 'with defaults' do
7
+ s = Settings.email(default: 'test@example.com')
8
+ s.should eq 'test@example.com'
9
+ Settings.get(:email).to_s.should eq 'test@example.com'
10
+ end
11
11
  end