mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
9dfef5a46b
Allow store accessors to be overrided like other attribute methods, e.g.: class User < ActiveRecord::Base store :settings, accessors: [ :color, :homepage ], coder: JSON def color super || 'red' end end
39 lines
1,007 B
Ruby
39 lines
1,007 B
Ruby
class Admin::User < ActiveRecord::Base
|
|
class Coder
|
|
def initialize(default = {})
|
|
@default = default
|
|
end
|
|
|
|
def dump(o)
|
|
ActiveSupport::JSON.encode(o || @default)
|
|
end
|
|
|
|
def load(s)
|
|
s.present? ? ActiveSupport::JSON.decode(s) : @default.clone
|
|
end
|
|
end
|
|
|
|
belongs_to :account
|
|
store :settings, :accessors => [ :color, :homepage ]
|
|
store_accessor :settings, :favorite_food
|
|
store :preferences, :accessors => [ :remember_login ]
|
|
store :json_data, :accessors => [ :height, :weight ], :coder => Coder.new
|
|
store :json_data_empty, :accessors => [ :is_a_good_guy ], :coder => Coder.new
|
|
|
|
def phone_number
|
|
read_store_attribute(:settings, :phone_number).gsub(/(\d{3})(\d{3})(\d{4})/,'(\1) \2-\3')
|
|
end
|
|
|
|
def phone_number=(value)
|
|
write_store_attribute(:settings, :phone_number, value && value.gsub(/[^\d]/,''))
|
|
end
|
|
|
|
def color
|
|
super || 'red'
|
|
end
|
|
|
|
def color=(value)
|
|
value = 'blue' unless %w(black red green blue).include?(value)
|
|
super
|
|
end
|
|
end
|