module Orphanage::ClassMethods

Public Instance Methods

adopt_options() click to toggle source

def home_model

@@home_model

end # parent

def home_model= home

@@home_model = home.to_s.titleize.constantize

end

# File lib/orphanage.rb, line 88
def adopt_options
  @@adopt_options
end
adopt_options=(options) click to toggle source
# File lib/orphanage.rb, line 92
def adopt_options= options
  @@adopt_options = options
end
orphan(options = {}) click to toggle source
# File lib/orphanage.rb, line 47
def orphan(options = {})
  # declare a class to be an orphan record class
  # home(string or symbol): lower case singular name of table the orphan
    # class will be adopted into. Example :exam or  "exam"

  # fks(array of strings or symbolds) array of foreign key names the orphan will use to attempt
    # to move into the home table.

  # options (hash)
    # destroy_on_adopt (bool): if the orphan record should be destroyed after successful
      # adoption
    # update_timestamps (hash)
      # created: (bool) if created_at timestamp should be updated
        # at adoption
      # updated (bool): if updated_at timestamp should be updated
        # at adoption

  default_options = {
    home: self.name.gsub('Temp', '').constantize,
    destroy_on_adopt: false,
    update_timestamps: {
      created: true,
      updated: true
    }
  }

  merged_options = default_options.deep_merge options

  # self.home_model = home
  self.adopt_options = merged_options

end