Class: Puffer::Component::Base

Inherits:
AbstractController::Base
  • Object
show all
Extended by:
ClassMethods
Includes:
AbstractController::Layouts, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, ActionController::Helpers, 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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

controller_path

Constructor Details

#initialize(field) ⇒ Base

Returns a new instance of Base.



41
42
43
44
# File 'lib/puffer/component.rb', line 41

def initialize field
  super()
  @field = field
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



37
38
39
# File 'lib/puffer/component.rb', line 37

def field
  @field
end

#identiferObject (readonly)

Returns the value of attribute identifer.



37
38
39
# File 'lib/puffer/component.rb', line 37

def identifer
  @identifer
end

#optsObject (readonly)

Returns the value of attribute opts.



37
38
39
# File 'lib/puffer/component.rb', line 37

def opts
  @opts
end

#parent_controllerObject (readonly)

Returns the value of attribute parent_controller.



37
38
39
# File 'lib/puffer/component.rb', line 37

def parent_controller
  @parent_controller
end

#recordObject (readonly)

Returns the value of attribute record.



37
38
39
# File 'lib/puffer/component.rb', line 37

def record
  @record
end

#recordsObject (readonly)

Returns the value of attribute records.



37
38
39
# File 'lib/puffer/component.rb', line 37

def records
  @records
end

#resourceObject (readonly)

Returns the value of attribute resource.



37
38
39
# File 'lib/puffer/component.rb', line 37

def resource
  @resource
end

Instance Method Details

#component_idObject



98
99
100
# File 'lib/puffer/component.rb', line 98

def component_id
  "component_#{identifer}"
end

#event_options(name) ⇒ Object



94
95
96
# File 'lib/puffer/component.rb', line 94

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



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

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

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



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

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

#fallback_action(action_name) ⇒ Object



82
83
84
# File 'lib/puffer/component.rb', line 82

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



69
70
71
72
73
74
75
76
# File 'lib/puffer/component.rb', line 69

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



78
79
80
# File 'lib/puffer/component.rb', line 78

def method_for_action action_name
  super fallback_action(action_name)
end

#process(context, parent_controller, record, options = {}) ⇒ Object



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

def process context, parent_controller, record, options = {}
  @parent_controller = parent_controller
  @record = record
  @opts = options
  @identifer = params[:identifer] || generate_identifer

  resource_params = params
  resource_params.merge!(:id => record.id) if record && record.respond_to?(:id)
  @resource = Puffer::Resource.new(resource_params, parent_controller)

  super context
end

#render(*args, &block) ⇒ Object



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

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



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

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