Class: RFO::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/rfo/base.rb

Overview

This is a main Form Object class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Constructor:

Arguments

  • hash - where key is a name and value is an object. If key is an AR object or Draper object then key name will become instance variable with value defined in hash value.

Run also assign_defaults method to set default values



20
21
22
23
24
25
26
27
28
# File 'lib/rfo/base.rb', line 20

def initialize(*args)
  options = args.extract_options!
  options.each_pair do |name, obj|
    next unless active_record_object?(obj)
    instance_variable_set("@#{name}", obj)
    super obj.attributes.symbolize_keys.slice(*self.attributes.keys)
  end
  assign_defaults
end

Class Method Details

.model_class(name) ⇒ Object

We can specify Form Object to act as some class

Example

# Form Object class SomeForm < RFO::Base

model_class Foo

attribute :name, String

end

# In view we can use it to generate default paths in form.

simple_form_for @some_form do |f|

= f.input :name
= f.submit


47
48
49
# File 'lib/rfo/base.rb', line 47

def self.model_class(name)
  @model_name = name || self
end

.model_nameObject

Required for defining paths in rails



54
55
56
57
# File 'lib/rfo/base.rb', line 54

def self.model_name
  @model_name ||= self
  ActiveModel::Name.new(@model_name, nil)
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rfo/base.rb', line 59

def persisted?
  false
end

#saveObject



63
64
65
66
67
68
69
# File 'lib/rfo/base.rb', line 63

def save
  if valid?
    persist!
  else
    false
  end
end

#update_attributes(attributes_hash) ⇒ Object



71
72
73
74
# File 'lib/rfo/base.rb', line 71

def update_attributes(attributes_hash)
  self.attributes = attributes_hash
  self.save
end