Class: MDWA::DSL::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/mdwa/dsl/action.rb

Constant Summary collapse

ACTION_TYPES =
[:collection, :member]
ALLOWED_METHODS =
[:get, :post, :put, :delete]
PREDEFINED_REQUEST_TYPES =
[:html, :ajax, :ajax_js, :modalbox]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, name, type, options = {}) ⇒ Action

Returns a new instance of Action.



15
16
17
18
19
20
21
22
23
# File 'lib/mdwa/dsl/action.rb', line 15

def initialize(entity, name, type, options = {})
  self.name         = name.to_sym
  self.type         = type.to_sym
  self.entity       = entity
  self.method       = options[:method] || :get
  self.request_type = options[:request_type] || :html
  self.response     = options[:response] || {}
  self.resource     = options[:resource] || false
end

Instance Attribute Details

#entityObject

Returns the value of attribute entity.



13
14
15
# File 'lib/mdwa/dsl/action.rb', line 13

def entity
  @entity
end

#methodObject

Returns the value of attribute method.



13
14
15
# File 'lib/mdwa/dsl/action.rb', line 13

def method
  @method
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/mdwa/dsl/action.rb', line 13

def name
  @name
end

#request_typeObject

Returns the value of attribute request_type.



13
14
15
# File 'lib/mdwa/dsl/action.rb', line 13

def request_type
  @request_type
end

#resourceObject

Returns the value of attribute resource.



13
14
15
# File 'lib/mdwa/dsl/action.rb', line 13

def resource
  @resource
end

#responseObject

Returns the value of attribute response.



13
14
15
# File 'lib/mdwa/dsl/action.rb', line 13

def response
  @response
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/mdwa/dsl/action.rb', line 13

def type
  @type
end

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mdwa/dsl/action.rb', line 45

def collection?
  self.type.to_sym == :collection
end

#generate_controllerObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mdwa/dsl/action.rb', line 66

def generate_controller
  action_str = []
  action_str << "\t# #{entity.generator_model.plural_name}##{self.name.to_s}"
  action_str << "\t# Route: #{generate_route}"
  action_str << "\tdef #{self.name.to_s}"
    
    if member?
      action_str << "\t\t@#{entity.generator_model.singular_name} = #{entity.generator_model.klass}.find(params[:id])"
      action_str << ""
    end
    
    action_str << "\t\trespond_to do |format|"        
    self.request_type.each do |request|          
      case request.to_sym
      when :modalbox
        action_str << "\t\t\tformat.html{render :layout => false}"
      when :html
        action_str << "\t\t\tformat.html #{"{#{self.response[:html]}}" unless self.response[:html].blank?}"
      when :ajax
        action_str << "\t\t\tformat.js #{"{#{self.response[:ajax]}}" unless self.response[:ajax].blank?}"
      when :ajax_js
        action_str << "\t\t\tformat.json #{"{#{self.response[:ajax_js]}}" unless self.response[:ajax_js].blank?}"
      else
        action_str << "\t\t\tformat.#{request.to_s} #{"{#{self.response[request.to_sym]}}" unless self.response[request.to_sym].blank?}"
      end
    end
    action_str << "\t\tend"
  
  action_str << "\tend"        
  action_str.join("\n")
end

#generate_routeObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mdwa/dsl/action.rb', line 53

def generate_route
  str = []
  str << "#{self.method.to_s} "
  str << "'#{self.entity.name.underscore.pluralize}"
  str << '/:id' if member?
  str << "/#{self.name.to_s}' "
  str << "=> '#{self.entity.name.underscore.pluralize}##{self.name.to_sym}'"
  str << ", :as => '"
  str << "#{self.name.to_s}_#{member? ? self.entity.name.underscore.singularize : self.entity.name.underscore.pluralize}" # action_entity(s)
  str << "'"
  str.join
end

#member?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mdwa/dsl/action.rb', line 41

def member?
  self.type.to_sym == :member
end

#resource?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mdwa/dsl/action.rb', line 49

def resource?
  resource
end

#template_namesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mdwa/dsl/action.rb', line 99

def template_names
  names = {}
  self.request_type.each do |request|
    case request.to_sym
    when :modalbox
      names[:modalbox] = "#{self.name}.html.erb"
    when :html
      names[:html] = "#{self.name}.html.erb" if self.response[:html].blank?
    when :ajax
      names[:ajax] = "#{self.name}.js.erb" if self.response[:ajax].blank?
    when :ajax_js
      names[:ajax_js] = "#{self.name}.json.erb" if self.response[:ajax_js].blank?
    else
      names[request.to_sym] = "#{self.name}.#{request.to_s}.erb" if self.response[request.to_sym].blank?
    end
  end
  
  return names
end