Module: Hammock::ActiveRecordPatches::InstanceMethods
- Defined in:
- lib/hammock/monkey_patches/active_record.rb
Instance Method Summary collapse
- #adjust(attrs) ⇒ Object
- #base_model ⇒ Object
- #concise_inspect ⇒ Object
- #description ⇒ Object
- #id_str ⇒ Object
- #new_or_deleted_before_save? ⇒ Boolean
-
#offset!(attribute, offset) ⇒ Object
Offset
attribute
byoffset
atomically in SQL. - #record? ⇒ Boolean
- #resource ⇒ Object
- #resource? ⇒ Boolean
- #resource_name ⇒ Object
- #resource_sym ⇒ Object
- #set_new_or_deleted_before_save ⇒ Object
-
#touch(*attrs) ⇒ Object
Updates each given attribute to the current time.
-
#touch_once(*attrs) ⇒ Object
Updates each given attribute to the current time, skipping attributes that are already set.
- #unsaved_attributes ⇒ Object
- #without_timestamps(&block) ⇒ Object
Instance Method Details
#adjust(attrs) ⇒ Object
185 186 187 188 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 185 def adjust attrs attrs.each {|k,v| send "#{k}=", v } save false end |
#base_model ⇒ Object
143 144 145 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 143 def base_model self.class.base_model end |
#concise_inspect ⇒ Object
112 113 114 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 112 def concise_inspect "#{self.class}<#{self.to_param || 'new'}>" end |
#description ⇒ Object
139 140 141 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 139 def description new_record? ? "new_#{base_model}" : "#{base_model}_#{id}" end |
#id_str ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 131 def id_str if new_record? "new_#{base_model}" else "#{base_model}_#{id}" end end |
#new_or_deleted_before_save? ⇒ Boolean
147 148 149 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 147 def new_or_deleted_before_save? @new_or_deleted_before_save end |
#offset!(attribute, offset) ⇒ Object
Offset attribute
by offset
atomically in SQL.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 198 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
128 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 128 def record?; true end |
#resource ⇒ Object
116 117 118 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 116 def resource self.class.resource end |
#resource? ⇒ Boolean
129 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 129 def resource?; false end |
#resource_name ⇒ Object
124 125 126 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 124 def resource_name self.class.resource_name end |
#resource_sym ⇒ Object
120 121 122 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 120 def resource_sym self.class.resource_sym end |
#set_new_or_deleted_before_save ⇒ Object
150 151 152 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 150 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.
168 169 170 171 172 173 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 168 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.
181 182 183 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 181 def touch_once *attrs touch *attrs.select {|attribute| attributes[attribute.to_s].nil? } end |
#unsaved_attributes ⇒ Object
190 191 192 193 194 195 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 190 def unsaved_attributes self.changed.inject({}) {|hsh,k| hsh[k] = attributes[k] hsh } end |
#without_timestamps(&block) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/hammock/monkey_patches/active_record.rb', line 154 def &block saved_value = self.class. self.class. = false returning yield do self.class. = saved_value end end |