rails_admin_settings 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-gemset +1 -0
  3. data/.ruby-version +2 -1
  4. data/CHANGELOG.md +11 -0
  5. data/app/models/rails_admin_settings/setting.rb +27 -5
  6. data/app/views/rails_admin/main/_setting_value.html.haml +1 -1
  7. data/config/locales/{en.rails_admin_settings.yml → en.yml} +0 -0
  8. data/config/locales/{ru.rails_admin_settings.yml → ru.yml} +20 -20
  9. data/lib/rails_admin_settings/dumper.rb +12 -0
  10. data/lib/rails_admin_settings/engine.rb +3 -0
  11. data/lib/rails_admin_settings/fallback.rb +19 -0
  12. data/lib/rails_admin_settings/hex_color_validator.rb +1 -1
  13. data/lib/rails_admin_settings/namespaced.rb +201 -0
  14. data/lib/rails_admin_settings/rails_admin_config.rb +3 -6
  15. data/lib/rails_admin_settings/railtie.rb +7 -0
  16. data/lib/rails_admin_settings/settings.rb +63 -165
  17. data/lib/rails_admin_settings/tasks.rb +25 -0
  18. data/lib/rails_admin_settings/types.rb +15 -1
  19. data/lib/rails_admin_settings/validation.rb +2 -4
  20. data/lib/rails_admin_settings/version.rb +1 -1
  21. data/lib/rails_admin_settings.rb +4 -0
  22. data/rails_admin_settings.gemspec +8 -7
  23. data/spec/carrierwave_spec.rb +7 -2
  24. data/spec/database_trickery_spec.rb +5 -7
  25. data/spec/defaults_spec.rb +53 -0
  26. data/spec/enabling_spec.rb +29 -0
  27. data/spec/label_spec.rb +2 -2
  28. data/spec/namespaced_spec.rb +41 -0
  29. data/spec/paperclip_spec.rb +5 -0
  30. data/spec/settings_spec.rb +13 -4
  31. data/spec/spec_helper.rb +4 -0
  32. data/spec/support/defaults.yml +15 -0
  33. data/spec/support/defaults_w_file.yml +19 -0
  34. data/spec/support/mongoid.yml +1 -1
  35. data/spec/types_spec.rb +12 -9
  36. metadata +49 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: daf283cef242208c445d469dfa8b3608cbbef8a1
4
- data.tar.gz: 7f8b0fa36cba674dddea4b6ab1e626701057bd28
3
+ metadata.gz: 7026b8ca9636dabaf5e279d2b2bfff9b6117c7bd
4
+ data.tar.gz: 22f40dc0852ea497da0826dfe612b5ccd721099d
5
5
  SHA512:
6
- metadata.gz: 87550cd78de0107a324dcce90b8094ad8787eb1336ca76709ec703273432f4e5b968e2bc806adcaec7386dafeeb90152cb2acda84f5476d72ab2ebd019154cac
7
- data.tar.gz: 1a28c1b482f2b65e56a98f089bb2c64643c55998047567a0e8ca5e1bfb36935aadb36045907a8375a581c9098dc75354bdfa3ad344c4f79bef3afc93e923e46a
6
+ metadata.gz: 98a6fe6a9ecb47a3cd5ebe3bb8b64014eb9bbe7e59e315a6dccd428d4d324d9e3bc42ef3e87d44532a693d72c697d8bf8f27fe67be59cb18f841c5cf381b148d
7
+ data.tar.gz: a0075c3be9b521493b03bf4b22767caf40761d13d4dab24a1f71f1ca1e50ed6dc2eb39b59a6d6fcc054b5468e188e0718715cc1a335b04cef6778900d718e327
data/.ruby-gemset CHANGED
@@ -1 +1,2 @@
1
1
  rails_admin_settings
2
+
data/.ruby-version CHANGED
@@ -1 +1,2 @@
1
- 2.0.0-p247
1
+ 2.0.0-p353
2
+
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## 0.6.0
2
+
3
+ - Added namespaced settings
4
+ - Added loading of default settings from config/settings.yml
5
+ - Settings.label(key) is removed
6
+ - Added Settings.apply_defaults! to load settings from yml file without
7
+ overwriting current settings.
8
+ *note*: If setting type is changed and old value does not pass validation for
9
+ new type, value will be reset to nil.
10
+ - Added rake settings:save_defaults to dump current settings to
11
+ config/defaults.yml
@@ -16,9 +16,14 @@ module RailsAdminSettings
16
16
 
17
17
  field :type, type: String, default: RailsAdminSettings.types.first
18
18
 
19
+ field :ns, type: String, default: 'main'
19
20
  field :key, type: String
20
- field :raw, type: String, default: ''
21
- field :label
21
+ index({ns: 1, key: 1}, {unique: true, sparse: true})
22
+
23
+ field :raw, type: String
24
+ field :label, type: String
25
+
26
+ scope :ns, ->(ns) { where(ns: ns) }
22
27
 
23
28
  include RailsAdminSettings::RequireHelpers
24
29
  include RailsAdminSettings::Processing
@@ -33,11 +38,28 @@ module RailsAdminSettings
33
38
  enabled
34
39
  end
35
40
 
36
- before_save do
37
- self.label = self.key unless self.label.present?
41
+ def name
42
+ label.blank? ? key : label
38
43
  end
39
44
 
40
- index(key: 1)
45
+ def to_path
46
+ if value.nil?
47
+ nil
48
+ else
49
+ URI.parse(value).path
50
+ end
51
+ end
52
+
53
+ def as_yaml(options = {})
54
+ v = {type: type, enabled: enabled, label: label}
55
+ if upload_type?
56
+ v[:value] = to_path
57
+ else
58
+ v[:value] = raw
59
+ end
60
+ v.stringify_keys!
61
+ v
62
+ end
41
63
 
42
64
  if Object.const_defined?('RailsAdmin')
43
65
  include RailsAdminSettings::RailsAdminConfig
@@ -1,7 +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
3
  - if @object.type == 'color'
4
- = form.text_field :raw, :value => field.value, 'data-color' => true
4
+ = form.text_field :raw, :value => field.value, 'data-color' => true, style: "background-color: ##{field.value}"
5
5
  - elsif @object.type == 'text' || @object.type == 'yaml'
6
6
  = form.text_area :raw, :value => field.value, :rows => 10, :cols => 80
7
7
  - elsif @object.type == 'html'
@@ -1,20 +1,20 @@
1
- ru:
2
- admin:
3
- settings:
4
- label: 'Настройки'
5
- no_ckeditor_detected: CKEditor не обнаружен — отображаю настройку как текстовое поле
6
- phone_invalid: Неверный телефонный номер
7
- email_invalid: Неверный email
8
- yaml_invalid: Неверный синтаксис YAML
9
- color_invalid: Неверный цвет
10
- mongoid:
11
- models:
12
- rails_admin_settings/setting: Настройки
13
-
14
- attributes:
15
- rails_admin_settings/setting:
16
- label: Название
17
- key: Ключ
18
- raw: Значение
19
- type: Тип
20
-
1
+ ru:
2
+ admin:
3
+ settings:
4
+ label: 'Настройки'
5
+ no_ckeditor_detected: CKEditor не обнаружен — отображаю настройку как текстовое поле
6
+ phone_invalid: Неверный телефонный номер
7
+ email_invalid: Неверный email
8
+ yaml_invalid: Неверный синтаксис YAML
9
+ color_invalid: Неверный цвет
10
+ mongoid:
11
+ models:
12
+ rails_admin_settings/setting: Настройки
13
+
14
+ attributes:
15
+ rails_admin_settings/setting:
16
+ label: Название
17
+ key: Ключ
18
+ raw: Значение
19
+ type: Тип
20
+
@@ -0,0 +1,12 @@
1
+ module RailsAdminSettings
2
+ module Dumper
3
+ def self.dump(path)
4
+ ns = {}
5
+ RailsAdminSettings::Setting.each do |s|
6
+ ns[s.ns] = {} if ns[s.ns].nil?
7
+ ns[s.ns][s.key] = s.as_yaml
8
+ end
9
+ File.write(path, ns.to_yaml)
10
+ end
11
+ end
12
+ end
@@ -8,6 +8,9 @@ module RailsAdminSettings
8
8
  after_filter { Settings.unload! }
9
9
  end
10
10
  end
11
+
12
+ RailsAdminSettings::Setting.where(:ns.exists => false).update_all(ns: 'main')
13
+ Settings.apply_defaults!(Rails.root.join('config/settings.yml'))
11
14
  end
12
15
  end
13
16
  end
@@ -0,0 +1,19 @@
1
+ module RailsAdminSettings
2
+ # we are inheriting from BasicObject so we don't get a bunch of methods from
3
+ # Kernel or Object
4
+ class Fallback < BasicObject
5
+ def initialize(ns, fb)
6
+ @ns = ns
7
+ @fb = fb
8
+ end
9
+
10
+ def inspect
11
+ "#<RailsAdminSettings::Fallback ns: #{@ns.inspect}, fb: #{@fb.inspect}>"
12
+ end
13
+
14
+ def method_missing(*args)
15
+ @ns.fallback = @fb
16
+ @ns.__send__(*args)
17
+ end
18
+ end
19
+ end
@@ -1,7 +1,7 @@
1
1
  module RailsAdminSettings
2
2
  class HexColorValidator < ActiveModel::EachValidator
3
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)
4
+ record.errors[attribute] << (options[:message] || I18n.t('admin.settings.color_invalid')) unless value.blank? || self.class.matches?(value)
5
5
  end
6
6
  def self.matches?(value)
7
7
  return false unless value
@@ -0,0 +1,201 @@
1
+ module RailsAdminSettings
2
+ # we are inheriting from BasicObject so we don't get a bunch of methods from
3
+ # Kernel or Object
4
+ class Namespaced < BasicObject
5
+ attr_accessor :settings, :fallback
6
+ attr_reader :loaded, :mutex
7
+
8
+ def initialize(name)
9
+ self.settings = {}
10
+ @mutex = ::Mutex.new
11
+ @loaded = false
12
+ @locked = false
13
+ @name = name
14
+ end
15
+
16
+ def nil?
17
+ false
18
+ end
19
+ def inspect
20
+ "#<RailsAdminSettings::Namespaced name: #{@name.inspect}, fallback: #{@fallback.inspect}, loaded: #{@loaded}>"
21
+ end
22
+ def pretty_inspect
23
+ inspect
24
+ end
25
+
26
+ def load!
27
+ mutex.synchronize do
28
+ return if loaded
29
+ @loaded = true
30
+ @settings = {}
31
+ ::RailsAdminSettings::Setting.ns(@name).each do |setting|
32
+ @settings[setting.key] = setting
33
+ end
34
+ end
35
+ end
36
+
37
+ def unload!
38
+ mutex.synchronize do
39
+ @loaded = false
40
+ @settings = {}
41
+ end
42
+ end
43
+
44
+ # returns setting object
45
+ def get(key, options = {})
46
+ load!
47
+ key = key.to_s
48
+ mutex.synchronize do
49
+ @locked = true
50
+ v = @settings[key]
51
+ if v.nil?
52
+ unless @fallback.nil?
53
+ v = ::Settings.ns(@fallback).getnc(key)
54
+ end
55
+ if v.nil?
56
+ v = set(key, options[:default], options)
57
+ end
58
+ end
59
+ @locked = false
60
+ v
61
+ end
62
+ end
63
+
64
+ # returns setting object
65
+ def getnc(key)
66
+ load!
67
+ mutex.synchronize do
68
+ self.settings[key]
69
+ end
70
+ end
71
+
72
+ def set(key, value = nil, options = {})
73
+ load! unless @locked
74
+ key = key.to_s
75
+ options.symbolize_keys!
76
+
77
+ if !options[:type].nil? && options[:type] == 'yaml' && !value.nil?
78
+ if value.class.name != 'String'
79
+ value = value.to_yaml
80
+ end
81
+ end
82
+
83
+ options.merge!(value: value)
84
+ if @locked
85
+ write_to_database(key, options)
86
+ else
87
+ mutex.synchronize do
88
+ write_to_database(key, options)
89
+ end
90
+ end
91
+ end
92
+
93
+ def enabled?(key, options = {})
94
+ get(key, options).enabled?
95
+ end
96
+
97
+ def []=(key, value)
98
+ set(key, value)
99
+ end
100
+ def [](key)
101
+ get(key)
102
+ end
103
+
104
+ def destroy!(key)
105
+ load!
106
+ key = key.to_s
107
+ mutex.synchronize do
108
+ ::RailsAdminSettings::Setting.where(ns: @name, key: key).destroy_all
109
+ @settings.delete(key)
110
+ end
111
+ end
112
+
113
+ def destroy_all!
114
+ mutex.synchronize do
115
+ ::RailsAdminSettings::Setting.where(ns: @name).destroy_all
116
+ @loaded = false
117
+ @settings = {}
118
+ end
119
+ end
120
+
121
+ # returns processed setting value
122
+ def method_missing(key, *args)
123
+ key = key.to_s
124
+ if key.end_with?('_enabled?')
125
+ key = key[0..-10]
126
+ v = get(key)
127
+ if v.nil?
128
+ set(key, '').enabled
129
+ else
130
+ v.enabled
131
+ end
132
+ elsif key.end_with?('_enabled=')
133
+ key = key[0..-10]
134
+ v = get(key)
135
+ if ::Mongoid::VERSION >= "4.0.0"
136
+ v.set(enabled: args.first)
137
+ else
138
+ v.set("enabled", args.first)
139
+ end
140
+ v.enabled
141
+ elsif key.end_with?('=')
142
+ key = key[0..-2]
143
+ options = args[1] || {}
144
+ value = args.first
145
+ set(key, value, options).val
146
+ else
147
+ v = get(key, args.first || {})
148
+ if v.nil?
149
+ ''
150
+ else
151
+ v.val
152
+ end
153
+ end
154
+ end
155
+
156
+ def write_to_database(key, options)
157
+ is_file = !options[:type].nil? && (options[:type] == 'image' || options[:type] == 'file')
158
+ if is_file
159
+ options[:raw] = ''
160
+ file = options[:value]
161
+ else
162
+ options[:raw] = options[:value]
163
+ end
164
+
165
+ options.delete(:value)
166
+ options[:ns] = @name
167
+
168
+ if @settings[key].nil?
169
+ options.delete(:overwrite)
170
+ v = ::RailsAdminSettings::Setting.create(options.merge(key: key))
171
+ if !v.persisted?
172
+ if v.errors[:key].any?
173
+ v = ::RailsAdminSettings::Setting.where(key: key).first
174
+ if v.nil?
175
+ ::Kernel.raise ::RailsAdminSettings::PersistenceException, 'Fatal: error in key and not in DB'
176
+ end
177
+ else
178
+ ::Kernel.raise ::RailsAdminSettings::PersistenceException, v.errors.full_messages.join(',')
179
+ end
180
+ end
181
+ @settings[key] = v
182
+ else
183
+ opts = options.dup
184
+ if options[:overwrite] == false
185
+ opts.delete(:raw)
186
+ opts.delete(:value)
187
+ opts.delete(:enabled)
188
+ end
189
+ opts.delete(:overwrite)
190
+ @settings[key].update_attributes!(opts)
191
+ end
192
+ if is_file
193
+ if options[:overwrite] != false || !@settings[key].file?
194
+ @settings[key].file = file
195
+ @settings[key].save!
196
+ end
197
+ end
198
+ @settings[key]
199
+ end
200
+ end
201
+ end
@@ -5,19 +5,16 @@ module RailsAdminSettings
5
5
  base.rails_admin do
6
6
  navigation_label I18n.t('admin.settings.label')
7
7
 
8
- object_label_method do
9
- :label
10
- end
11
-
12
8
  list do
13
9
  if Object.const_defined?('RailsAdminToggleable')
14
10
  field :enabled, :toggle
15
11
  else
16
12
  field :enabled
17
13
  end
18
- field :label
19
- field :raw
20
14
  field :type
15
+ field :ns
16
+ field :name
17
+ field :raw
21
18
  end
22
19
 
23
20
  edit do
@@ -0,0 +1,7 @@
1
+ module RailsAdminSettings
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ require File.expand_path('../tasks', __FILE__)
5
+ end
6
+ end
7
+ end