Module: Encodable::ActiveRecord::ClassMethods

Defined in:
lib/encodable/active_record/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_encodable_attribute(method, value, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/encodable/active_record/class_methods.rb', line 28

def add_encodable_attribute(method, value, options = {})
  value = "#{options[:prefix]}_#{value}" if options[:prefix]
  method = method.to_sym
  value = value.to_sym
  renamed_encoded_attributes(options[:as]).merge!({method => value}) if method != value
  # Un-black-list any attribute we white-listed
  unencodable_attributes(options[:as]).delete method
  default_attributes(options[: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
# File 'lib/encodable/active_record/class_methods.rb', line 4

def attr_encodable(*attributes)
  options = extract_encodable_options!(attributes)

  unless @encodable_whitelist_started
    # Since we're white-listing, make sure we black-list every attribute to begin with
    unencodable_attributes(options[:as]).push *column_names.map(&:to_sym)
    @encodable_whitelist_started = true
  end

  attributes.each do |attribute|
    if attribute.is_a?(Hash)
      attribute.each do |method, value|
        add_encodable_attribute(method, value, options)
      end
    else
      add_encodable_attribute(attribute, attribute, options)
    end
  end

  if options[:as] != :default
    scope options[:as], select(default_attributes(options[:as]).reject{|attribute| !column_names.include?(attribute.to_s)})
  end
end

#attr_unencodable(*attributes) ⇒ Object



38
39
40
41
# File 'lib/encodable/active_record/class_methods.rb', line 38

def attr_unencodable(*attributes)
  options = extract_encodable_options!(attributes)
  unencodable_attributes(options[:as]).push *attributes.map(&:to_sym)
end

#default_attributes(name = nil) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/encodable/active_record/class_methods.rb', line 43

def default_attributes(name = nil)
  @default_attributes ||= merge_encodable_superclass_options(:default_attributes, [])
  if name
    @default_attributes[name] ||= []
  else
    @default_attributes
  end
end

#encodable_setsObject



52
53
54
# File 'lib/encodable/active_record/class_methods.rb', line 52

def encodable_sets
  @encodable_sets
end

#renamed_encoded_attributes(name = nil) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/encodable/active_record/class_methods.rb', line 56

def renamed_encoded_attributes(name = nil)
  @renamed_encoded_attributes ||= merge_encodable_superclass_options(:renamed_encoded_attributes, {})
  if name
    @renamed_encoded_attributes[name] ||= {}
  else
    @renamed_encoded_attributes
  end
end

#unencodable_attributes(name = nil) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/encodable/active_record/class_methods.rb', line 65

def unencodable_attributes(name = nil)
  @unencodable_attributes ||= merge_encodable_superclass_options(:unencodable_attributes, [])
  if name
    @unencodable_attributes[name] ||= []
  else
    @unencodable_attributes
  end
end