Module: Mcfly::Model::ClassMethods

Defined in:
lib/mcfly/has_mcfly.rb

Instance Method Summary collapse

Instance Method Details

#has_mcfly(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mcfly/has_mcfly.rb', line 57

def has_mcfly(options = {})
  send :include, InstanceMethods

  before_validation :record_validation
  before_destroy    :allow_destroy if options[:append_only]

  # FIXME: :created_dt should also be readonly.  However, we set
  # it for debugging purposes.  Should consider making this
  # readonly once we're in production.  Also, :user_id should be
  # read-only.  We should only set whodunnit and let PostgreSQL
  # set it.
  attr_readonly :group_id, :obsoleted_dt, :o_user_id #, :user_id
end

#mcfly_belongs_to(name, options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mcfly/has_mcfly.rb', line 117

def mcfly_belongs_to(name, options = {})
  validates_with Mcfly::Model::AssociationValidator, field: name
  belongs_to(name, options)

  # Store child associations for the parent category
  # e.g. if HedgeCost is adding a belong_to assoc to HedgeCostCategory
  # then add HedgeCost and FK to the @@associations array
  self.reflect_on_all_associations.each do |a|
    if a.name == name
      a.klass.class_variable_set(:@@associations, []) unless
        a.klass.class_variable_defined?(:@@associations)

      a.klass.class_variable_get(:@@associations) <<
        [a.active_record, a.foreign_key]
    end
  end
end

#mcfly_lookup(name, options = {}, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mcfly/has_mcfly.rb', line 71

def mcfly_lookup(name, options = {}, &block)
  delorean_fn(name, options) do |ts, *args|
    raise "time cannot be nil" if ts.nil?

    ts = Mcfly.normalize_infinity(ts)

    self.where("#{table_name}.obsoleted_dt >= ? AND " +
               "#{table_name}.created_dt < ?", ts, ts).scoping do
      block.call(ts, *args)
    end
  end
end

#mcfly_validates_uniqueness_of(*attr_names) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mcfly/has_mcfly.rb', line 84

def mcfly_validates_uniqueness_of(*attr_names)
  # FIXME: this all looks somewhat hacky since it makes
  # assumptions about the shape of attr_names.  Should, at
  # least, add some assertions here to check the assumptions.

  # Set MCFLY_UNIQUENESS class constant to the args passed.
  # This is useful for introspection.  FIXME: won't work if
  # mcfly_validates_uniqueness_of is called multiple times on
  # the same class.
  attr_list =
    if attr_names.last.is_a?(Hash)
      attr_names[0..-2] + (attr_names.last[:scope] || [])
    else
      attr_names.clone
    end
  self.const_set(:MCFLY_UNIQUENESS, attr_list.freeze)

  # start building arguments to validates_uniqueness_of
  attr_names << {} unless attr_names.last.is_a?(Hash)

  attr_names.last[:scope] ||= []

  # add :obsoleted_dt to the uniqueness scope
  attr_names.last[:scope] << :obsoleted_dt

  # Set uniqueness error message if not set.  FIXME: need to
  # figure out how to change the base message.  It still
  # prepends the pluralized main attr.
  attr_names.last[:message] ||= "- record must be unique"

  validates_uniqueness_of(*attr_names)
end