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



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, 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
27
# File 'lib/encodable/active_record/class_methods.rb', line 4

def attr_encodable(*attributes)
  options = extract_encodable_options!(attributes)
  unless @encodable_whitelist_started && @encodable_whitelist_started.key?(options[:as])
    # 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 ||= {}
    @encodable_whitelist_started[options[:as]] = 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
    columns = default_attributes(options[:as]).reject{|attribute| !column_names.include?(attribute.to_s)}
    scope options[: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)
  options = extract_encodable_options!(attributes)
  unencodable_attributes(options[: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 ||= merge_encodable_superclass_options(:default_attributes, [])
  if name
    @default_attributes[name] ||= []
  else
    @default_attributes
  end
end

#encodable_setsObject



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 ||= merge_encodable_superclass_options(: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 ||= merge_encodable_superclass_options(:unencodable_attributes, [])
  if name
    @unencodable_attributes[name] ||= []
  else
    @unencodable_attributes
  end
end