module Rooftop::FieldAliases

Public Class Methods

included(base) click to toggle source
# File lib/rooftop/field_aliases.rb, line 7
def self.included(base)
  # Include Rooftop::HookCalls to allow us to push things into a list of hooks in the right order
  base.include Rooftop::HookCalls
  base.extend ClassMethods

  # Add the call to the :after_find hook to the list of hook calls, to be processed later.
  # This is where we iterate over our previously established list of field aliases.
  base.send(:add_to_hook, :after_find, ->(r){
    r.field_aliases.each do |old, new|
      if r.respond_to?(old)
        r.send("#{new}=",r.send(old))
      end
    end
  })

  base.send(:before_save, ->(r) {
    r.field_aliases.each do |old,new|
      r.send(:"restore_#{new}!") unless r.new?
    end
  })

end

Public Instance Methods

field_aliases() click to toggle source

Class method to get the class's field aliases

# File lib/rooftop/field_aliases.rb, line 43
def field_aliases
  self.class.instance_variable_get(:"@field_aliases") || {}
end