Class: Presenter
- Inherits:
-
Object
- Object
- Presenter
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, Enumerable
- Defined in:
- lib/liaison/presenter.rb
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access individual values as if this were the CGI params hash.
-
#add_errors(errs) ⇒ Object
Combine error messages from any ActiveModel object with the presenter’s, so they will show on the form.
- #class_with_model_name ⇒ Object (also: #class)
-
#each(&block) ⇒ Object
Presenter objects are instances of Enumerable, which means you can iterate over the keys and values as set by the form.
-
#initialize(model_name, opts = {}) ⇒ Presenter
constructor
A new instance of Presenter.
-
#persisted? ⇒ Boolean
This only exists for ActiveModel::Constructs.
-
#with_params(params = {}) ⇒ Object
Set the params from the form using this.
Constructor Details
#initialize(model_name, opts = {}) ⇒ Presenter
Returns a new instance of Presenter.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/liaison/presenter.rb', line 19 def initialize(model_name, opts = {}) @model_name = model_name @validator = opts[:validator] @fields = opts[:fields] if @fields && !@fields.empty? (class << self; self; end).send(:attr_accessor, *@fields) end end |
Instance Method Details
#[](key) ⇒ Object
Access individual values as if this were the CGI params hash.
@sign_up_presenter[:account_name]
82 83 84 |
# File 'lib/liaison/presenter.rb', line 82 def [](key) to_hash[key] end |
#add_errors(errs) ⇒ Object
Combine error messages from any ActiveModel object with the presenter’s, so they will show on the form.
@sign_up_presenter.add_errors(account.errors)
You will probably use it like this:
class SignUp
attr_accessor :presenter
def save
account = Account.new
account.save.tap do |succeeded|
presenter.add_errors(account.errors) unless succeeded
end
end
end
75 76 77 |
# File 'lib/liaison/presenter.rb', line 75 def add_errors(errs) errs.each {|k,v| errors.add(k,v)} end |
#class_with_model_name ⇒ Object Also known as: class
31 32 33 34 35 36 37 38 39 |
# File 'lib/liaison/presenter.rb', line 31 def class_with_model_name # :nodoc: self.class_without_model_name.tap do |c| c.instance_variable_set('@_model_name', @model_name) (class << c; self; end).send(:define_method,:model_name) do model_namer = Struct.new(:name).new(self.instance_variable_get('@_model_name')) ActiveModel::Name.new(model_namer) end end end |
#each(&block) ⇒ Object
Presenter objects are instances of Enumerable, which means you can iterate over the keys and values as set by the form.
@sign_up_presenter.each {|k,v| puts "the form set #{k} to #{v}" }
90 91 92 |
# File 'lib/liaison/presenter.rb', line 90 def each(&block) to_hash.each(&block) end |
#persisted? ⇒ Boolean
This only exists for ActiveModel::Constructs
44 45 46 |
# File 'lib/liaison/presenter.rb', line 44 def persisted? # :nodoc: false end |
#with_params(params = {}) ⇒ Object
Set the params from the form using this.
For example, if you have a presenter in the @sign_up_presenter variable, you can update the values in it using this method:
@sign_up_presenter.with_params(params[:sign_up])
54 55 56 57 |
# File 'lib/liaison/presenter.rb', line 54 def with_params(params = {}) params.each {|k,v| self.send("#{k}=", v)} self end |