Module: Encodable::ActiveRecord::ClassMethods
- Defined in:
- lib/encodable/active_record/class_methods.rb
Instance Method Summary collapse
- #add_encodable_attribute(method, value, options = {}) ⇒ Object
- #attr_encodable(*attributes) ⇒ Object
- #attr_unencodable(*attributes) ⇒ Object
- #default_attributes(name = nil) ⇒ Object
- #encodable_sets ⇒ Object
- #renamed_encoded_attributes(name = nil) ⇒ Object
- #unencodable_attributes(name = nil) ⇒ Object
Instance Method Details
#add_encodable_attribute(method, value, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/encodable/active_record/class_methods.rb', line 29 def add_encodable_attribute(method, value, = {}) value = "#{[:prefix]}_#{value}" if [:prefix] method = method.to_sym value = value.to_sym renamed_encoded_attributes([:as]).merge!({method => value}) if method != value # Un-black-list any attribute we white-listed unencodable_attributes([:as]).delete method default_attributes([:as]).push method end |
#attr_encodable(*attributes) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/encodable/active_record/class_methods.rb', line 4 def attr_encodable(*attributes) = (attributes) unless @encodable_whitelist_started && @encodable_whitelist_started.key?([:as]) # Since we're white-listing, make sure we black-list every attribute to begin with unencodable_attributes([:as]).push *column_names.map(&:to_sym) @encodable_whitelist_started ||= {} @encodable_whitelist_started[[:as]] = true end attributes.each do |attribute| if attribute.is_a?(Hash) attribute.each do |method, value| add_encodable_attribute(method, value, ) end else add_encodable_attribute(attribute, attribute, ) end end if [:as] != :default columns = default_attributes([:as]).reject{|attribute| !column_names.include?(attribute.to_s)} scope [:as], select(columns.map {|column| "#{table_name}.#{column}" }) end end |
#attr_unencodable(*attributes) ⇒ Object
39 40 41 42 |
# File 'lib/encodable/active_record/class_methods.rb', line 39 def attr_unencodable(*attributes) = (attributes) unencodable_attributes([:as]).push *attributes.map(&:to_sym) end |
#default_attributes(name = nil) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/encodable/active_record/class_methods.rb', line 44 def default_attributes(name = nil) @default_attributes ||= (:default_attributes, []) if name @default_attributes[name] ||= [] else @default_attributes end end |
#encodable_sets ⇒ Object
53 54 55 |
# File 'lib/encodable/active_record/class_methods.rb', line 53 def encodable_sets @encodable_sets end |
#renamed_encoded_attributes(name = nil) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/encodable/active_record/class_methods.rb', line 57 def renamed_encoded_attributes(name = nil) @renamed_encoded_attributes ||= (:renamed_encoded_attributes, {}) if name @renamed_encoded_attributes[name] ||= {} else @renamed_encoded_attributes end end |
#unencodable_attributes(name = nil) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/encodable/active_record/class_methods.rb', line 66 def unencodable_attributes(name = nil) @unencodable_attributes ||= (:unencodable_attributes, []) if name @unencodable_attributes[name] ||= [] else @unencodable_attributes end end |