Class: Rfm::Base

Inherits:
Record show all
Extended by:
Config
Defined in:
lib/rfm/base.rb

Overview

Adds ability to create Rfm::Base model classes that behave similar to ActiveRecord::Base models. If you set your Rfm.config (or RFM_CONFIG) with your host, database, account, password, and any other server/database options, you can provide your models with nothing more than a layout.

class Person < Rfm::Base config :layout => ‘mylayout’ end

And similar to ActiveRecord, you can define callbacks, validations, attributes, and methods on your model.

(if you have ActiveModel loaded).

class Account < Rfm::Base
  config :layout=>'account_xml'
  before_create :encrypt_password
  validates :email, :presence => true
  validates :username, :presence => true
  attr_accessor :password
end

Then in your project, you can use these models just like ActiveRecord models. The query syntax and options are still Rfm under the hood. Treat your model classes like Rfm::Layout objects, with a few enhancements.

@account = Account.new :username => 'bill', :password => 'pass'
@account.email = '[email protected]'
@account.save!

@person = Person.find({:name => 'mike'}, :max_records => 50)[0]
@person.update_attributes(:name => 'Michael', :title => "Senior Partner")
@person.save

Constant Summary

Constants included from Config

Config::CONFIG_DONT_STORE, Config::CONFIG_KEYS

Instance Attribute Summary

Attributes inherited from Record

#layout, #mod_id, #portals, #record_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config

config, config_clear, get_config, log, state

Methods inherited from Record

#[], #[]=, #field_names, #initialize, new, #replace_with_fresh_data, #respond_to?

Methods inherited from CaseInsensitiveHash

#[], #[]=

Methods inherited from Hash

#_create_accessor, #_merge_object!, #rfm_filter, #rfm_only, #to_cih

Constructor Details

This class inherits a constructor from Rfm::Record

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



118
119
120
# File 'lib/rfm/base.rb', line 118

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

.config(*args) ⇒ Object



76
77
78
# File 'lib/rfm/base.rb', line 76

def config(*args)
	super(*args){|options| @config.merge!(:layout=>options[:strings][0]) if options[:strings] && options[:strings][0]}
end

.create(*args) ⇒ Object

New record, save, (with callbacks & validations if ActiveModel is loaded)



123
124
125
# File 'lib/rfm/base.rb', line 123

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

.edit(*args) ⇒ Object

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



128
129
130
# File 'lib/rfm/base.rb', line 128

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.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rfm/base.rb', line 105

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rfm/base.rb', line 81

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
name = get_config[:layout] || 'test'   # The 'test' was added to help active-model-lint tests pass.
@layout = Rfm::Factory.layout(name, self) #.sublayout
	
	# Added by wbr to give config heirarchy: layout -> model -> sublayout
	#config :parent=>'parent_layout'
	#config :parent=>'Rfm::Config'
	#@layout.config model
	#@layout.config :parent=>self
	
@layout.model = self
@layout
end

Instance Method Details

#destroyObject

Delete record from database, with callbacks & validations.



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

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)


235
236
237
# File 'lib/rfm/base.rb', line 235

def destroyed?
	@destroyed
end

#new_record?Boolean

Is this a newly created record, not saved yet?

Returns:

  • (Boolean)


140
141
142
# File 'lib/rfm/base.rb', line 140

def new_record?
	return true if (self.record_id.nil? || self.record_id.empty?)
end

#persisted?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/rfm/base.rb', line 244

def persisted?
	record_id ? true : false
end

#reload(force = false) ⇒ Object

Reload record from database TODO: handle error when record has been deleted TODO: Move this to Rfm::Record.



147
148
149
150
151
152
# File 'lib/rfm/base.rb', line 147

def reload(force=false)
 if (@mods.empty? or force) and record_id
 	@mods.clear
   self.replace_with_fresh_data layout.find(self.record_id)[0]   #self.class.find(self.record_id)
  end
end

#saveObject

Same as save!, but will not raise error.



211
212
213
214
215
216
# File 'lib/rfm/base.rb', line 211

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.



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

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_modifiedObject

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



219
220
221
# File 'lib/rfm/base.rb', line 219

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

#to_keyObject



248
249
250
# File 'lib/rfm/base.rb', line 248

def to_key
	record_id ? [record_id] : nil
end

#to_modelObject

For ActiveModel compatibility



240
241
242
# File 'lib/rfm/base.rb', line 240

def to_model
	self
end

#to_paramObject



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

def to_param
	record_id
end

#to_partial_path(object = self) ⇒ Object

@object)



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rfm/base.rb', line 51

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.



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rfm/base.rb', line 155

def update_attributes(new_attr)
	new_attr.each do |k,v|
		k = k.to_s.downcase
		if keys.include?(k)
			@mods[k] = v
			self[k] = v
		else
			instance_variable_set("@#{k}", v)
		end
	end
end

#update_attributes!(new_attr) ⇒ Object

Mass update of record attributes, with saving.



194
195
196
197
# File 'lib/rfm/base.rb', line 194

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