Class: Puffer::Component::Base

Inherits:
AbstractController::Base
  • Object
show all
Extended by:
ClassMethods
Includes:
AbstractController::Helpers, AbstractController::Layouts, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, ActionController::RequestForgeryProtection, ActionController::UrlFor
Defined in:
lib/puffer/component.rb

Overview

Puffer::Component::Base is a base component low-level for puffer fields. It provides all the base functionality for rendering visual fields components. Every component belongs to field by field’s type Every component should have some basic actions, based on parent controller fieldsets, so they can be fallbacked as fieldsets.

Also, every component can have additional actions for ajax requests handling and component state changing

Defined Under Namespace

Modules: ClassMethods, ComponentHelper

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

controller_path, render_component

Constructor Details

#initialize(field) ⇒ Base

Returns a new instance of Base.



63
64
65
66
# File 'lib/puffer/component.rb', line 63

def initialize field
  super()
  @field = field
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



59
60
61
# File 'lib/puffer/component.rb', line 59

def field
  @field
end

#identiferObject

Returns the value of attribute identifer.



59
60
61
# File 'lib/puffer/component.rb', line 59

def identifer
  @identifer
end

#optsObject

Returns the value of attribute opts.



59
60
61
# File 'lib/puffer/component.rb', line 59

def opts
  @opts
end

#parent_controllerObject

Returns the value of attribute parent_controller.



59
60
61
# File 'lib/puffer/component.rb', line 59

def parent_controller
  @parent_controller
end

#resourceObject

Returns the value of attribute resource.



59
60
61
# File 'lib/puffer/component.rb', line 59

def resource
  @resource
end

Instance Method Details

#component_idObject



131
132
133
# File 'lib/puffer/component.rb', line 131

def component_id
  "component_#{identifer}"
end

#event_options(name) ⇒ Object



119
120
121
# File 'lib/puffer/component.rb', line 119

def event_options name
  {:action => :event, :event => name, :field => field.to_s, :fieldset => field.field_set.name, :identifer => identifer}
end

#event_path(name, options = {}) ⇒ Object



115
116
117
# File 'lib/puffer/component.rb', line 115

def event_path name, options = {}
  resource.collection_path (options || {}).merge(event_options(name))
end

#event_url(name, options = {}) ⇒ Object



111
112
113
# File 'lib/puffer/component.rb', line 111

def event_url name, options = {}
  resource.collection_url (options || {}).merge(event_options(name))
end

#fallback_action(action_name) ⇒ Object



107
108
109
# File 'lib/puffer/component.rb', line 107

def fallback_action action_name
  ((parent_controller._fieldset_fallbacks[action_name] || []).detect {|name| action_method? name.to_s} || action_name).to_s
end

#javascript_wrap(type, html) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/puffer/component.rb', line 94

def javascript_wrap type, html
  case type
  when :replace then
    "$('#{component_id}').replace('#{view_context.escape_javascript html}')".html_safe
  else
    html
  end
end

#method_for_action(action_name) ⇒ Object



103
104
105
# File 'lib/puffer/component.rb', line 103

def method_for_action action_name
  fallback_action(action_name) || super
end

#process(parent_controller, context, *args) ⇒ Object



68
69
70
71
# File 'lib/puffer/component.rb', line 68

def process parent_controller, context, *args
  @parent_controller = parent_controller
  super context, *args
end

#recordObject



123
124
125
# File 'lib/puffer/component.rb', line 123

def record
  @record || instance_variable_get("@#{resource.name.singularize}")
end

#recordsObject



127
128
129
# File 'lib/puffer/component.rb', line 127

def records
  @records || instance_variable_get("@#{resource.name.pluralize}")
end

#render(*args, &block) ⇒ Object



84
85
86
87
88
# File 'lib/puffer/component.rb', line 84

def render *args, &block
  options = _normalize_render(*args, &block)
  options[:template] = fallback_action(options[:template])
  render_to_body(options).html_safe
end

#replace(*args) ⇒ Object



90
91
92
# File 'lib/puffer/component.rb', line 90

def replace *args
  javascript_wrap :replace, render(*args)
end

#send_action(method_name, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/puffer/component.rb', line 73

def send_action method_name, *args
  @opts = args.extract_options!
  if @opts[:record]
    @resource = Puffer::Resource.new(params.merge(:member => @opts[:record]), parent_controller)
  else
    @resource = parent_controller.resource
  end
  @identifer = params[:identifer] || generate_identifer
  send method_name, *args
end