Class: Waw::ActionController::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/waw/controllers/action/action.rb

Overview

A Web action, inside an ActionController.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, signature, routing, controller, method) ⇒ Action

Creates an action instance



21
22
23
24
25
# File 'lib/waw/controllers/action/action.rb', line 21

def initialize(name, signature, routing, controller, method)
  @name, @signature, @routing = name, signature, routing
  @controller, @method = controller, method
  @routing = ::Waw::Routing::ActionRouting.new unless @routing
end

Instance Attribute Details

#controllerObject (readonly)

Action controller



9
10
11
# File 'lib/waw/controllers/action/action.rb', line 9

def controller
  @controller
end

#nameObject (readonly)

Action name



12
13
14
# File 'lib/waw/controllers/action/action.rb', line 12

def name
  @name
end

#routingObject (readonly)

Action routing



18
19
20
# File 'lib/waw/controllers/action/action.rb', line 18

def routing
  @routing
end

#signatureObject (readonly)

Action signature (pre-conditions and validation)



15
16
17
# File 'lib/waw/controllers/action/action.rb', line 15

def signature
  @signature
end

Instance Method Details

#ajax_action_call(opts = {}) ⇒ Object

Factors a javascript call to the associated action



73
74
75
76
# File 'lib/waw/controllers/action/action.rb', line 73

def ajax_action_call(opts = {})
  form_id = opts[:form_id] || id
  "#{id}($(\"form##{form_id}\").serialize(), \"form##{form_id}\");"
end

#ajax_action_codeObject

Factors the ajax code for the action itself



47
48
49
50
51
52
53
54
55
# File 'lib/waw/controllers/action/action.rb', line 47

def ajax_action_code
  js = Waw::ActionController::JSGeneration.new
  code = js.generate_js_for_action(Waw::kernel, self, "")
  <<-EOF
    <script type="text/javascript">
      #{code}
    </script>
  EOF
end

#ajax_form_preparer(opts = {}) ⇒ Object

Factors the ajax code for preparing a formulary



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/waw/controllers/action/action.rb', line 58

def ajax_form_preparer(opts = {})
  form_id = opts[:form_id] || id
  <<-EOF
    <script type="text/javascript">
    	$(document).ready(function() {
    	  $("form##{form_id}").submit(function() {
    	    #{id}($("form##{form_id}").serialize(), "form##{form_id}");
      	  return false;
    	  });
      });
    </script>
  EOF
end

#ajax_form_submit(opts = {}) ⇒ Object

Factors the ajax code to submit the formulary



79
80
81
82
# File 'lib/waw/controllers/action/action.rb', line 79

def ajax_form_submit(opts = {})
  form_id = opts[:form_id] || id
  "javascript:$('form##{form_id}').submit()"
end

Factors the ajax link for invoking this action in a <a onclick=“…”>



40
41
42
43
44
# File 'lib/waw/controllers/action/action.rb', line 40

def ajax_link(arguments = {})
  buffer = ""
  arguments.each_pair {|k, v| buffer << ", '#{k}' : '#{v}'"}
  "javascript:#{id}({#{buffer[2..-1]}}, '##{id}')"
end

#execute(params = {}) ⇒ Object

Executes the action inside a controller and using parameters



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/waw/controllers/action/action.rb', line 85

def execute(params = {})
  ok, values = @signature.apply(params)
  if ok
    # validation is ok, merge params and continue
    [:success, @method.bind(controller.instance).call(params.merge!(values))]
  else
    # validation is ko
    [:"validation-ko", values]
  end
rescue ::Waw::Validation::KO => ex
  [:"validation-ko", ex.failed]
rescue ::Waw::Validation::Error => ex
  [:"validation-error", ex.failed]
end

#href(params = nil) ⇒ Object Also known as: url, uri

Builds a href for this action, with optional parameters



33
34
35
# File 'lib/waw/controllers/action/action.rb', line 33

def href(params = nil)
  "#{controller.url}/#{name}" << (params ? "?#{params.to_url_query}" : "")
end

#idObject

Returns a unique id for this action in the architecture



28
29
30
# File 'lib/waw/controllers/action/action.rb', line 28

def id
  "#{controller.url}/#{name}"[1..-1].gsub('/', '_')
end