Module: Hammock::ActiveRecordPatches::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#adjust(attrs) ⇒ Object



186
187
188
189
# File 'lib/hammock/monkey_patches/active_record.rb', line 186

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

#adjust_attributes(attrs) ⇒ Object



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

def adjust_attributes attrs
  self.attributes = attrs
  changed.empty? || update_attributes(attrs)
end

#base_modelObject



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

def base_model
  self.class.base_model
end

#concise_inspectObject



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

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

#descriptionObject



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

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

#id_strObject



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

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

#new_or_deleted_before_save?Boolean

Returns:

  • (Boolean)


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

def new_or_deleted_before_save?
  @new_or_deleted_before_save
end

#offset!(attribute, offset) ⇒ Object

Offset attribute by offset atomically in SQL.



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

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)


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

def record?; true end

#resourceObject



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

def resource
  self.class.resource
end

#resource?Boolean

Returns:

  • (Boolean)


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

def resource?; false end

#resource_nameObject



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

def resource_name
  self.class.resource_name
end

#resource_symObject



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

def resource_sym
  self.class.resource_sym
end

#set_new_or_deleted_before_saveObject



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

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.



169
170
171
172
173
174
# File 'lib/hammock/monkey_patches/active_record.rb', line 169

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.



182
183
184
# File 'lib/hammock/monkey_patches/active_record.rb', line 182

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

#unsaved_attributesObject



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

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

#without_timestamps(&block) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/hammock/monkey_patches/active_record.rb', line 155

def without_timestamps &block
  saved_value = self.class.record_timestamps
  self.class.record_timestamps = false
  returning yield do
    self.class.record_timestamps = saved_value
  end
end