Class: ActionController::Base

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/rails_ext.rb

Direct Known Subclasses

ApplicationController

Instance Method Summary collapse

Instance Method Details

#find_selected(name) ⇒ Object

Returns the selected object from a select_pane and defines the selected? method on it to return true (so that define_selected will work properly



29
30
31
32
33
34
35
36
37
# File 'app/controllers/rails_ext.rb', line 29

def find_selected(name)
  array = instantiate_array_from_hashes(@params[name])
  selected = @params["#{name}_selected"]
  selected_object = array.find { |o| o.class.name == selected }
  def selected_object.selected?
    true
  end
  selected_object
end

#instantiate_array_from_hashes(class_name_2_attr_hash_hash) ⇒ Object

Instantiates an Array of object from class_name_2_attr_hash_hash which should be a hash where the keys are class names and the values a Hash containing => attr_value pairs.



8
9
10
11
12
13
14
# File 'app/controllers/rails_ext.rb', line 8

def instantiate_array_from_hashes(class_name_2_attr_hash_hash)
  result = []
  class_name_2_attr_hash_hash.each do |class_name, attr_hash|
    result << instantiate_from_hash(eval(class_name), attr_hash)
  end
  result
end

#instantiate_from_hash(clazz, attr_hash) ⇒ Object



16
17
18
19
20
21
22
23
# File 'app/controllers/rails_ext.rb', line 16

def instantiate_from_hash(clazz, attr_hash)
  object = clazz.new
  attr_hash.each do |attr_name, attr_value|
    setter = attr_name[1..-1] + "="
    object.__send__(setter.to_sym, attr_value) if object.respond_to?(setter.to_sym)
  end
  object
end