Class: Rfm::Base

Inherits:
Record show all
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

Instance Method Summary collapse

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rfm/base.rb', line 181

def initialize(record={}, resultset_obj=[], field_meta='', 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



271
272
273
# File 'lib/rfm/base.rb', line 271

def any(*args)
  layout.any(*args)[0]
end

.config(*args) ⇒ Object



233
234
235
# File 'lib/rfm/base.rb', line 233

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)



276
277
278
# File 'lib/rfm/base.rb', line 276

def create(*args)
  new(*args).send :create
end

.edit(*args) ⇒ Object

Using this method will skip callbacks. Use instance method #update instead



281
282
283
# File 'lib/rfm/base.rb', line 281

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.



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rfm/base.rb', line 258

def find(find_criteria, options={})
	#puts "base.find-#{layout.object_id}"
  r = layout.find(find_criteria, options)
  if ![Hash,Array].include?(find_criteria.class) and r.size == 1
    r[0]
  else
    r
   end
 rescue Rfm::Error::RecordMissingError
   nil
end

.layoutObject

Access/create the layout object associated with this model



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rfm/base.rb', line 238

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



225
226
227
228
229
230
231
# File 'lib/rfm/base.rb', line 225

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_layoutObject

Access the parent layout of this model



253
254
255
# File 'lib/rfm/base.rb', line 253

def parent_layout
	layout.parent_layout
end

Instance Method Details

#destroyObject

Delete record from database, with callbacks & validations.



360
361
362
363
364
365
366
367
368
369
# File 'lib/rfm/base.rb', line 360

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

Returns:

  • (Boolean)


371
372
373
# File 'lib/rfm/base.rb', line 371

def destroyed?
	@destroyed
end

#new_record?Boolean

Is this a newly created record, not saved yet?

Returns:

  • (Boolean)


293
294
295
# File 'lib/rfm/base.rb', line 293

def new_record?
	return true if self.record_id.blank?
end

#persisted?Boolean

Returns:

  • (Boolean)


380
381
382
# File 'lib/rfm/base.rb', line 380

def persisted?
	record_id ? true : false
end

#reload(force = false) ⇒ Object

Reload record from database TODO: handle error when record has been deleted



299
300
301
302
303
304
# File 'lib/rfm/base.rb', line 299

def reload(force=false)
 if (@mods.empty? or force) and record_id
 	@mods.clear
   self.replace self.class.find(self.record_id)
  end
end

#saveObject

Same as save!, but will not raise error.



347
348
349
350
351
352
# File 'lib/rfm/base.rb', line 347

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.



337
338
339
340
341
342
343
344
# File 'lib/rfm/base.rb', line 337

def save!
  #raise "Record Invalid" unless valid? rescue nil
  if @record_id
    self.update
  else
    self.create
  end
end

#save_if_not_modifiedObject

Just like Layout#save_if_not_modified, but with callbacks & validations.



355
356
357
# File 'lib/rfm/base.rb', line 355

def save_if_not_modified
  update(@mod_id) if @mods.size > 0
end

#to_keyObject



384
385
386
# File 'lib/rfm/base.rb', line 384

def to_key
	record_id ? [record_id] : nil
end

#to_modelObject

For ActiveModel compatibility



376
377
378
# File 'lib/rfm/base.rb', line 376

def to_model
	self
end

#to_paramObject



388
389
390
# File 'lib/rfm/base.rb', line 388

def to_param
	record_id
end

#to_partial_path(object = self) ⇒ Object

@object)



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rfm/base.rb', line 199

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.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rfm/base.rb', line 308

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.



331
332
333
334
# File 'lib/rfm/base.rb', line 331

def update_attributes!(new_attr)
  self.update_attributes(new_attr)
  self.save!
end