Module: Hammock::ActiveRecordPatches::InstanceMethods

Defined in:
lib/hammock/monkey_patches/active_record.rb

Instance Method Summary collapse

Instance Method Details

#adjust(attrs) ⇒ Object



193
194
195
196
# File 'lib/hammock/monkey_patches/active_record.rb', line 193

def adjust attrs
  attrs.each {|k,v| send "#{k}=", v }
  save false
end

#base_modelObject



145
146
147
# File 'lib/hammock/monkey_patches/active_record.rb', line 145

def base_model
  self.class.base_model
end

#concise_inspectObject



114
115
116
# File 'lib/hammock/monkey_patches/active_record.rb', line 114

def concise_inspect
  "#{self.class}<#{self.to_param || 'new'}>"
end

#descriptionObject



141
142
143
# File 'lib/hammock/monkey_patches/active_record.rb', line 141

def description
  new_record? ? "new_#{base_model}" : "#{base_model}_#{id}"
end

#id_strObject



133
134
135
136
137
138
139
# File 'lib/hammock/monkey_patches/active_record.rb', line 133

def id_str
  if new_record?
    "new_#{base_model}"
  else
    "#{base_model}_#{id}"
  end
end

#new_or_deleted_before_save?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/hammock/monkey_patches/active_record.rb', line 149

def new_or_deleted_before_save?
  @new_or_deleted_before_save
end

#offset!(attribute, offset) ⇒ Object

Offset attribute by offset atomically in SQL.



206
207
208
209
210
211
212
213
214
215
# File 'lib/hammock/monkey_patches/active_record.rb', line 206

def offset! attribute, offset
  if new_record?
    log "Can't offset! a new record."
  else
    # Update the in-memory model
    send "#{attribute}=", send(attribute) + offset
    # Update the DB
    run_updater_sql 'Offset', "#{connection.quote_column_name(attribute)} = #{connection.quote_column_name(attribute)} + #{quote_value(offset)}"
  end
end

#record?Boolean

Returns:

  • (Boolean)


130
# File 'lib/hammock/monkey_patches/active_record.rb', line 130

def record?; true end

#resourceObject



118
119
120
# File 'lib/hammock/monkey_patches/active_record.rb', line 118

def resource
  self.class.resource
end

#resource?Boolean

Returns:

  • (Boolean)


131
# File 'lib/hammock/monkey_patches/active_record.rb', line 131

def resource?; false end

#resource_nameObject



126
127
128
# File 'lib/hammock/monkey_patches/active_record.rb', line 126

def resource_name
  self.class.resource_name
end

#resource_symObject



122
123
124
# File 'lib/hammock/monkey_patches/active_record.rb', line 122

def resource_sym
  self.class.resource_sym
end

#set_new_or_deleted_before_saveObject



152
153
154
# File 'lib/hammock/monkey_patches/active_record.rb', line 152

def set_new_or_deleted_before_save
  @new_or_deleted_before_save = new_record? || send_if_respond_to(:deleted?)
end

#touch(*attrs) ⇒ Object

Updates each given attribute to the current time.

Assumes that each column can accept a Time instance, i.e. that they’re all datetime columns or similar.

The updates are done with update_attribute, and as such they are done with callbacks but without validation.



176
177
178
179
180
181
# File 'lib/hammock/monkey_patches/active_record.rb', line 176

def touch *attrs
  now = Time.now
  attrs.each {|attribute|
    update_attribute attribute, now
  }
end

#touch_once(*attrs) ⇒ Object

Updates each given attribute to the current time, skipping attributes that are already set.

Assumes that each column can accept a Time instance, i.e. that they’re all datetime columns or similar.

The updates are done with update_attribute, and as such they are done with callbacks but without validation.



189
190
191
# File 'lib/hammock/monkey_patches/active_record.rb', line 189

def touch_once *attrs
  touch *attrs.select {|attribute| attributes[attribute.to_s].nil? }
end

#undestroyObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/hammock/monkey_patches/active_record.rb', line 156

def undestroy
  unless new_record?
    if frozen?
      self.class.find_with_deleted(self.id).undestroy # Re-fetch ourselves and undestroy the thawed copy
    else
      # We can undestroy
      return false if callback(:before_undestroy) == false
      result = self.class.update_all ['deleted_at = ?', (self.deleted_at = nil)], ['id = ?', self.id]
      callback(:after_undestroy)
      self if result != false
    end
  end
end

#unsaved_attributesObject



198
199
200
201
202
203
# File 'lib/hammock/monkey_patches/active_record.rb', line 198

def unsaved_attributes
  self.changed.inject({}) {|hsh,k|
    hsh[k] = attributes[k]
    hsh
  }
end