Class: Formic::Form

Inherits:
Base
  • Object
show all
Defined in:
lib/formic/form.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#content, #options, #page, #template

Instance Method Summary collapse

Methods inherited from Base

#add_class, default_template, #formics, #has_class?, #initialize, #merge_options, template, #to_s

Constructor Details

This class inherits a constructor from Formic::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(action, model, options = {}, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/formic/form.rb', line 46

def method_missing action, model, options={}, &block
  @action = action
  options[:title] ||= "#{action} #{model.class.to_s.underscore}".gsub('_', ' ').titleize
  return _initialize_with_model(
    model,
    options[:action] || "#{self.page.url_for(model)}/#{action}",
    "#{action}_#{model.class.to_s.downcase}_form",
    options,
    &block
  )
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



5
6
7
# File 'lib/formic/form.rb', line 5

def action
  @action
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/formic/form.rb', line 5

def model
  @model
end

Instance Method Details

#_initialize(options = {}, &block) ⇒ Object



7
8
9
10
# File 'lib/formic/form.rb', line 7

def _initialize options={}, &block
  super options, &block
  self.options[:method] ||= 'POST'
end

#_initialize_with_model(model, action, classname, options = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/formic/form.rb', line 58

def _initialize_with_model model, action, classname, options={}, &block
  options[:title] ||= "#{action} #{model.class.to_s.underscore}".gsub('_', ' ').titleize
  @model = model
  self.merge_options(options)
  self.options[:method] ||= 'POST'
  self.options[:action] ||= action
  self.options[:class].push classname
  self.content = block if block_given?
  return self
end

#create(model, options = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/formic/form.rb', line 19

def create model, options={}, &block
  options[:title] ||= "New #{model.class.to_s.underscore}".gsub('_', ' ').titleize
  @action = :create

  return _initialize_with_model(
    model,
    options[:action] || self.page.url_for(model),
    "create_#{model.class.to_s.downcase}_form",
    options,
    &block
  )
end

#edit(model, options = {}, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/formic/form.rb', line 32

def edit model, options={}, &block
  self.put

  @action = :edit
  options[:title] ||= "Edit #{model.class.to_s.underscore}".gsub('_', ' ').titleize
  return _initialize_with_model(
    model,
    options[:action] || self.page.url_for(model),
    "edit_#{model.class.to_s.downcase}_form",
    options,
    &block
  )
end

#put(options = {}, &block) ⇒ Object



12
13
14
15
16
17
# File 'lib/formic/form.rb', line 12

def put options={}, &block
  self.options[:method ] = 'POST'
  self.options[:_method] = 'PUT'
  self.content = block if block_given?
  return self
end