Class: Rfm::Base
- Extended by:
- Config
- Defined in:
- lib/rfm/base.rb
Overview
Hash
Instance Attribute Summary
Attributes inherited from Record
#layout, #mod_id, #portals, #record_id, #resultset
Class Method Summary collapse
-
.any(*args) ⇒ Object
Layout#any, returns single record, not resultset.
- .config(*args) ⇒ Object
-
.create(*args) ⇒ Object
New record, save, (with callbacks & validations if ActiveModel is loaded).
-
.edit(*args) ⇒ Object
Using this method will skip callbacks.
-
.find(find_criteria, options = {}) ⇒ Object
Just like Layout#find, but searching by record_id will return a record, not a resultset.
-
.layout ⇒ Object
Access/create the layout object associated with this model.
-
.new(*args) ⇒ Object
Build a new record without saving.
-
.parent_layout ⇒ Object
Access the parent layout of this model.
Instance Method Summary collapse
-
#destroy ⇒ Object
Delete record from database, with callbacks & validations.
- #destroyed? ⇒ Boolean
-
#initialize(record = {}, resultset_obj = [], field_meta = '', layout_obj = self.class.layout, portal = nil) ⇒ Base
constructor
A new instance of Base.
-
#new_record? ⇒ Boolean
Is this a newly created record, not saved yet?.
- #persisted? ⇒ Boolean
-
#reload(force = false) ⇒ Object
Reload record from database TODO: handle error when record has been deleted.
-
#save ⇒ Object
Same as save!, but will not raise error.
-
#save! ⇒ Object
Save record modifications to database (with callbacks & validations).
-
#save_if_not_modified ⇒ Object
Just like Layout#save_if_not_modified, but with callbacks & validations.
- #to_key ⇒ Object
-
#to_model ⇒ Object
For ActiveModel compatibility.
- #to_param ⇒ Object
-
#to_partial_path(object = self) ⇒ Object
@object).
-
#update_attributes(new_attr) ⇒ Object
Mass update of record attributes, without saving.
-
#update_attributes!(new_attr) ⇒ Object
Mass update of record attributes, with saving.
Methods included from Config
config, config_clear, get_config, sanitize_config
Methods inherited from Record
#[], #[]=, build_records, #field_names, #respond_to?
Methods inherited from CaseInsensitiveHash
Methods inherited from Hash
#rfm_filter, #rfm_only, #to_cih
Constructor Details
#initialize(record = {}, resultset_obj = [], field_meta = '', layout_obj = self.class.layout, portal = nil) ⇒ Base
Returns a new instance of Base.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rfm/base.rb', line 179 def initialize(record={}, resultset_obj=[], ='', layout_obj=self.class.layout, portal=nil) if resultset_obj == [] and !record.respond_to?(:columns) #.has_key? 'field' @mods = Rfm::CaseInsensitiveHash.new @layout = layout_obj @resultset = Resultset.allocate # loop thru each layout field, creating hash keys with nil values layout_obj.field_names.each do |field| field_name = field.to_s self[field_name] = nil end self.update_attributes(record) unless record == {} self.merge!(@mods) unless @mods == {} @loaded = true else super end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rfm::Record
Class Method Details
.any(*args) ⇒ Object
Layout#any, returns single record, not resultset
269 270 271 |
# File 'lib/rfm/base.rb', line 269 def any(*args) layout.any(*args)[0] end |
.config(*args) ⇒ Object
231 232 233 |
# File 'lib/rfm/base.rb', line 231 def config(*args) super(*args){|strings| @config.merge!(:layout=>strings[0]) if strings[0]} end |
.create(*args) ⇒ Object
New record, save, (with callbacks & validations if ActiveModel is loaded)
274 275 276 |
# File 'lib/rfm/base.rb', line 274 def create(*args) new(*args).send :create end |
.edit(*args) ⇒ Object
Using this method will skip callbacks. Use instance method #update
instead
279 280 281 |
# File 'lib/rfm/base.rb', line 279 def edit(*args) layout.edit(*args)[0] end |
.find(find_criteria, options = {}) ⇒ Object
Just like Layout#find, but searching by record_id will return a record, not a resultset.
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rfm/base.rb', line 256 def find(find_criteria, ={}) #puts "base.find-#{layout.object_id}" r = layout.find(find_criteria, ) if ![Hash,Array].include?(find_criteria.class) and r.size == 1 r[0] else r end rescue Rfm::Error::RecordMissingError nil end |
.layout ⇒ Object
Access/create the layout object associated with this model
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/rfm/base.rb', line 236 def layout return @layout if @layout cnf = get_config raise "Could not get :layout from get_config in Base.layout method" unless cnf[:layout] #return unless cnf[:layout] @layout = Rfm::Factory.layout(cnf).sublayout # Added by wbr to give config heirarchy: layout -> model -> sublayout config :parent=>'parent_layout' @layout.config :parent=>'model' @layout.model = self @layout end |
.new(*args) ⇒ Object
Build a new record without saving
223 224 225 226 227 228 229 |
# File 'lib/rfm/base.rb', line 223 def new(*args) # Without this method, infinite recursion will happen from Record.new #puts "Creating new record from BASE" rec = self.allocate rec.send(:initialize, *args) rec end |
.parent_layout ⇒ Object
Access the parent layout of this model
251 252 253 |
# File 'lib/rfm/base.rb', line 251 def parent_layout layout.parent_layout end |
Instance Method Details
#destroy ⇒ Object
Delete record from database, with callbacks & validations.
358 359 360 361 362 363 364 365 366 367 |
# File 'lib/rfm/base.rb', line 358 def destroy return unless record_id run_callbacks :destroy do self.class.delete(record_id) @destroyed = true @mods.clear end self.freeze #self end |
#destroyed? ⇒ Boolean
369 370 371 |
# File 'lib/rfm/base.rb', line 369 def destroyed? @destroyed end |
#new_record? ⇒ Boolean
Is this a newly created record, not saved yet?
291 292 293 |
# File 'lib/rfm/base.rb', line 291 def new_record? return true if self.record_id.blank? end |
#persisted? ⇒ Boolean
378 379 380 |
# File 'lib/rfm/base.rb', line 378 def persisted? record_id ? true : false end |
#reload(force = false) ⇒ Object
Reload record from database TODO: handle error when record has been deleted
297 298 299 300 301 |
# File 'lib/rfm/base.rb', line 297 def reload(force=false) if (@mods.empty? or force) and record_id self.replace self.class.find(self.record_id) end end |
#save ⇒ Object
Same as save!, but will not raise error.
345 346 347 348 349 350 |
# File 'lib/rfm/base.rb', line 345 def save save! rescue (self.errors[:base] rescue []) << $! return nil end |
#save! ⇒ Object
Save record modifications to database (with callbacks & validations). If record cannot be saved will raise error.
334 335 336 337 338 339 340 341 342 |
# File 'lib/rfm/base.rb', line 334 def save! #return unless @mods.size > 0 raise "Record Invalid" unless valid? rescue nil if @record_id self.update else self.create end end |
#save_if_not_modified ⇒ Object
Just like Layout#save_if_not_modified, but with callbacks & validations.
353 354 355 |
# File 'lib/rfm/base.rb', line 353 def save_if_not_modified update(@mod_id) if @mods.size > 0 end |
#to_key ⇒ Object
382 383 384 |
# File 'lib/rfm/base.rb', line 382 def to_key record_id ? [record_id] : nil end |
#to_model ⇒ Object
For ActiveModel compatibility
374 375 376 |
# File 'lib/rfm/base.rb', line 374 def to_model self end |
#to_param ⇒ Object
386 387 388 |
# File 'lib/rfm/base.rb', line 386 def to_param record_id end |
#to_partial_path(object = self) ⇒ Object
@object)
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/rfm/base.rb', line 197 def to_partial_path(object = self) #@object) return 'some/partial/path' ##### DISABLED HERE - ActiveModel Lint only needs a string ##### ##### TODO: implement to_partial_path to return meaningful string. @partial_names[object.class.name] ||= begin object = object.to_model if object.respond_to?(:to_model) object.class.model_name.partial_path.dup.tap do |partial| path = @view.controller_path partial.insert(0, "#{File.dirname(path)}/") if partial.include?(?/) && path.include?(?/) end end end |
#update_attributes(new_attr) ⇒ Object
Mass update of record attributes, without saving. TODO: return error or nil if input hash contains no recognizable keys.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/rfm/base.rb', line 305 def update_attributes(new_attr) # creates new special hash input_hash = Rfm::CaseInsensitiveHash.new # populate new hash with input, coercing keys to strings new_attr.each{|k,v| input_hash.merge! k.to_s=>v} # loop thru each layout field, adding data to @mods self.class.field_controls.keys.each do |field| field_name = field.to_s if input_hash.has_key?(field_name) @mods.merge! field_name=>(input_hash[field_name] || '') end end # loop thru each input key-value, # creating new attribute if key doesn't exist in model. input_hash.each do |k,v| if !self.class.field_controls.keys.include?(k) and self.respond_to?(k) self.instance_variable_set("@#{k}", v) end end self.merge!(@mods) unless @mods == {} end |
#update_attributes!(new_attr) ⇒ Object
Mass update of record attributes, with saving.
328 329 330 331 |
# File 'lib/rfm/base.rb', line 328 def update_attributes!(new_attr) self.update_attributes(new_attr) self.save! end |