Module: Orphanage::ClassMethods

Defined in:
lib/orphanage.rb

Overview

methods

Instance Method Summary collapse

Instance Method Details

#adopt_optionsObject

def home_model

@@home_model

end # parent

def home_model= home

@@home_model = home.to_s.titleize.constantize

end



88
89
90
# File 'lib/orphanage.rb', line 88

def adopt_options
  @@adopt_options
end

#adopt_options=(options) ⇒ Object

adopt_options



92
93
94
# File 'lib/orphanage.rb', line 92

def adopt_options= options
  @@adopt_options = options
end

#orphan(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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