has_setting 0.4.6 → 0.5

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.
@@ -5,6 +5,7 @@ class CreateSettings < ActiveRecord::Migration
5
5
  t.integer(:owner_id, :null => false)
6
6
  t.string(:name, :null => false, :length => 64)
7
7
  t.string(:value, :null => true, :length => 255)
8
+ t.string(:locale, :null => true)
8
9
  t.timestamps
9
10
  end
10
11
  end
@@ -1,26 +1,13 @@
1
1
  module HasSetting
2
-
3
- module InstanceMethods
4
- def write_setting(name, value)
5
- # find an existing setting or build a new one
6
- setting = self.settings.detect() {|item| item.name == name }
7
- setting = self.settings.build(:name => name) if setting.blank?
8
- setting.value = value
9
- end
10
-
11
- def read_setting(name)
12
- # use detect instead of SQL find. like this the 'cached' has_many-collection is inited
13
- # only once
14
- self.settings.detect() {|item| item.name == name }
15
- end
16
- end
17
-
2
+
3
+ extend ActiveSupport::Concern
4
+
18
5
  module ClassMethods
19
6
  # Setup of the getter/setter
20
7
  def has_setting(name, options = {})
21
8
  name = name.to_s
22
9
  raise ArgumentError.new('Setting name must not be blank') if name.blank?
23
-
10
+
24
11
  self.class_eval do
25
12
  unless @has_setting_options # define only once
26
13
  # AR association to settings
@@ -31,7 +18,7 @@ module HasSetting
31
18
  def self.has_setting_options
32
19
  @has_setting_options
33
20
  end
34
-
21
+
35
22
  private
36
23
  # Callback to save settings
37
24
  def save_has_setting_association
@@ -41,30 +28,44 @@ module HasSetting
41
28
  end
42
29
  end
43
30
  end
44
-
45
-
31
+
46
32
  raise ArgumentError.new("Setting #{name }is already defined on #{self.name}") if self.has_setting_options.has_key?(name)
47
-
33
+
48
34
  # default options
49
- options[:type] ||= :string # treat as string
35
+ type = options[:type] || :string # treat as string
50
36
  # default could be false, thats why we use has_key?
51
- options[:default] = options.has_key?(:default) ? options[:default] : nil # no default
37
+ default = options[:default] || (options.has_key?(:default) ? options[:default] : nil) # no default
52
38
  self.has_setting_options[name] = options
53
-
39
+
54
40
  # setter
55
41
  define_method("#{name}=".intern) do |value|
56
- formatter = HasSetting::Formatters.for_type(options[:type])
42
+ formatter = HasSetting::Formatters.for_type(type)
57
43
  write_setting(name, formatter.to_s(value))
58
44
  end
59
-
45
+
60
46
  # getter
61
47
  define_method(name) do |*args|
62
48
  setting = read_setting(name)
63
49
  options = args.first || self.class.has_setting_options[name]
64
50
  return options[:default] if setting.nil?
65
- formatter = Formatters.for_type(options[:type])
51
+ formatter = Formatters.for_type(options[:type] || type)
66
52
  formatter.to_type(setting.value)
67
53
  end
68
54
  end
69
55
  end
56
+
57
+ def write_setting(name, value)
58
+ # find an existing setting or build a new one
59
+ setting = self.settings.detect() {|item| item.name == name and item.locale.to_s == I18n.locale.to_s }
60
+ setting = self.settings.build(:name => name, locale: I18n.locale.to_s) if setting.blank?
61
+ setting.value = value
62
+ end
63
+
64
+ def read_setting(name)
65
+ # use detect instead of SQL find. like this the 'cached' has_many-collection is inited
66
+ # only once
67
+ s = self.settings.detect() {|item| item.name == name and item.locale.to_s == I18n.locale.to_s} # first see if there is a setting with current locale
68
+ s ||= self.settings.detect() {|item| item.name == name} # then if not found, take the first setting with matching name (TODO: add locale fallbacks)
69
+ s
70
+ end
70
71
  end
@@ -1,3 +1,3 @@
1
1
  module HasSetting
2
- VERSION = "0.4.6"
2
+ VERSION = "0.5"
3
3
  end
data/lib/has_setting.rb CHANGED
@@ -2,9 +2,10 @@
2
2
  require File.dirname(__FILE__) + '/has_setting/ar_extensions'
3
3
  require File.dirname(__FILE__) + '/has_setting/formatters'
4
4
  require File.dirname(__FILE__) + '/has_setting/setting'
5
- ActiveRecord::Base.class_eval do
6
- include(HasSetting::InstanceMethods)
7
- extend(HasSetting::ClassMethods)
5
+
6
+
7
+ ActiveSupport.on_load(:active_record) do
8
+ include HasSetting
8
9
  end
9
10
 
10
11
 
data/test/test_helper.rb CHANGED
@@ -23,13 +23,12 @@ ActiveRecord::Base.connection.create_table(:settings) do |table|
23
23
  table.string(:name, :limit => 64, :null => false)
24
24
  table.string(:owner_type, :limit => 255, :null => false)
25
25
  table.integer(:owner_id, :null => false)
26
+ table.string(:locale)
26
27
  end
27
28
  [:foos, :bars, :bazs].each do |table|
28
29
  ActiveRecord::Base.connection.create_table(table) do |t|
29
30
  end
30
31
  end
31
- ActiveRecord::Base.logger = Logger.new(STDOUT)
32
- ActiveRecord::Base.logger.level = Logger::DEBUG # change to DEBUG if you want to see something :-)
33
32
 
34
33
  require File.join(File.dirname(__FILE__), 'foo')
35
34
  require File.join(File.dirname(__FILE__), 'bar')
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper'
2
2
 
3
3
  class HasSettingTest < Test::Unit::TestCase
4
- def setup()
4
+ def setup
5
5
  @foo = Foo.create!
6
6
  @bar = Bar.create!
7
7
  @baz = Baz.create!
@@ -143,6 +143,16 @@ class HasSettingTest < Test::Unit::TestCase
143
143
  @foo.setting_4 = false
144
144
  assert_equal false, @foo.setting_4
145
145
  end
146
+
147
+ def test_locale_awareness
148
+ I18n.locale = :de
149
+ @bar.setting_1 = 'setting de'
150
+ I18n.locale = 'de-CH'
151
+ @bar.setting_1 = 'setting ch'
152
+ assert_equal 'setting ch', @bar.setting_1
153
+ I18n.locale = :de
154
+ assert_equal 'setting de', @bar.setting_1
155
+ end
146
156
 
147
157
 
148
158
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_setting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: '0.5'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: