Module: SettingAccessors::Integration

Defined in:
lib/setting_accessors/integration.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/setting_accessors/integration.rb', line 2

def self.included(base)
  base.validates_with SettingAccessors::IntegrationValidator

  # After the main record was saved, we can save its settings.
  # This is necessary as the record might have been a new record
  # without an ID yet
  base.after_save do
    settings.send(:persist!)
  end

  base.extend ClassMethods
end

Instance Method Details

#as_json(options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/setting_accessors/integration.rb', line 98

def as_json(options = {})
  json = super options

  setting_names = SettingAccessors::Internal.setting_accessor_names(self.class)
  if only = options[:only]
    setting_names &= Array(only).map(&:to_s)
  elsif except = options[:except]
    setting_names -= Array(except).map(&:to_s)
  end

  setting_names.each do |setting_name|
    json[setting_name.to_s] = send(setting_name)
  end
  json
end

#reloadObject

Previously read setting values have to be refreshed if a record is reloaded. Without this, #reload’ing a record would not update its setting values to the latest database version if they were previously read.

Example to demonstrate the problem with this override:

user = User.create(:a_boolean => true)
user_alias = User.find(user.id)
user.a_boolean = !user_alias.a_boolean
user.save
user_alias.reload
user_alias.a_boolean
#=> true


92
93
94
95
96
# File 'lib/setting_accessors/integration.rb', line 92

def reload(*)
  super
  @settings_accessor = nil
  self
end

#settingsObject



114
115
116
# File 'lib/setting_accessors/integration.rb', line 114

def settings
  @settings_accessor ||= SettingAccessors::Accessor.new(self)
end