Class: Rfm::Base
- Extended by:
- Config
- Defined in:
- lib/rfm/base.rb,
lib/rfm/utilities/scope.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 collapse
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
-
.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.
Instance Method Summary collapse
-
#destroy ⇒ Object
Delete record from database, with callbacks & validations.
- #destroyed? ⇒ Boolean
-
#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 TODO: Move this to Rfm::Record.
-
#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, 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){|| @config.merge!(:layout=>[:strings][0]) if [:strings] && [: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, ={}) #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
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 hierarchy: 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
#destroy ⇒ Object
Delete record from database, with callbacks & validations.
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/rfm/base.rb', line 225 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
236 237 238 |
# File 'lib/rfm/base.rb', line 236 def destroyed? @destroyed end |
#new_record? ⇒ Boolean
Is this a newly created record, not saved yet?
136 137 138 |
# File 'lib/rfm/base.rb', line 136 def new_record? return true if (self.record_id.nil? || self.record_id.empty?) end |
#persisted? ⇒ Boolean
245 246 247 |
# File 'lib/rfm/base.rb', line 245 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.
143 144 145 146 147 148 |
# File 'lib/rfm/base.rb', line 143 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 |
#save ⇒ Object
Same as save!, but will not raise error.
210 211 212 213 214 215 216 217 |
# File 'lib/rfm/base.rb', line 210 def save save! # rescue # (self.errors[:base] rescue []) << $! # return nil rescue nil end |
#save! ⇒ Object
Save record modifications to database (with callbacks & validations). If record cannot be saved will raise error.
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rfm/base.rb', line 196 def save! #return unless @mods.size > 0 raise "Record Invalid" unless valid? rescue nil if @record_id self.update else self.create end rescue (self.errors[:base] rescue []) << $! raise $! end |
#save_if_not_modified ⇒ Object
Just like Layout#save_if_not_modified, but with callbacks & validations.
220 221 222 |
# File 'lib/rfm/base.rb', line 220 def save_if_not_modified update(@mod_id) if @mods.size > 0 end |
#to_key ⇒ Object
249 250 251 |
# File 'lib/rfm/base.rb', line 249 def to_key record_id ? [record_id] : nil end |
#to_model ⇒ Object
For ActiveModel compatibility
241 242 243 |
# File 'lib/rfm/base.rb', line 241 def to_model self end |
#to_param ⇒ Object
253 254 255 |
# File 'lib/rfm/base.rb', line 253 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.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rfm/base.rb', line 151 def update_attributes(new_attr) new_attr.each do |k,v| k = k.to_s.downcase if key?(k) || (layout.field_keys.include?(k.split('.')[0]) rescue nil) @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.
190 191 192 193 |
# File 'lib/rfm/base.rb', line 190 def update_attributes!(new_attr) self.update_attributes(new_attr) self.save! end |