Module: E9Polls::PollsHelper

Defined in:
app/helpers/e9_polls/polls_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_associated_resource(association_name) ⇒ Object



104
105
106
107
108
# File 'app/helpers/e9_polls/polls_helper.rb', line 104

def build_associated_resource(association_name)
  params_method = "#{association_name}_build_parameters"
  build_params = resource_class.send(params_method) if resource_class.respond_to?(params_method)
  resource.send(association_name).build(build_params || {})
end


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/e9_polls/polls_helper.rb', line 13

def e9_polls_resource_link(action, record, opts = {})
  opts.symbolize_keys!

  # NOTE cascading backend for i18n would accomplish the lookup without defaults

  action  = action.to_sym
  klass   = record.is_a?(Class) ? record : record.class
  scope   = "e9_polls.#{klass.model_name.collection}"

  # NOTE this assumes the definition of #parent, which is added by IR but only on controllers
  #      with polymorphic belongs_to relationships (by default).
  scopes  = [*(opts[:scope] || @route_scope), parent].compact
  path    = case action.to_sym
            when :new;  new_polymorphic_path(scopes << klass)
            when :edit; edit_polymorphic_path(scopes << record)
            else        polymorphic_path(scopes << record)
            end
  
  if action == :destroy
    opts[:method] = :delete
    opts.reverse_merge!({
      :remote  => true, 
      :confirm => t(:"#{scope}.confirm_destroy", :default => :"e9_polls.confirm_destroy")
    })
  end

  link_to t(:"#{scope}.#{action}", :default => :"e9_polls.#{action}"), path, opts
end

#html_concat(*chunks) ⇒ Object

Util



7
8
9
10
11
# File 'app/helpers/e9_polls/polls_helper.rb', line 7

def html_concat(*chunks)
  ''.html_safe.tap do |html|
    chunks.each {|chunk| html.safe_concat("#{chunk}\n") }
  end
end


55
56
57
# File 'app/helpers/e9_polls/polls_helper.rb', line 55

def link_to_add_nested_attribute(association_name)
  link_to( t(:add_nested_attribute, :scope => :e9_polls), '#', :class => 'add-nested-association', 'data-association' => association_name)
end


59
60
61
# File 'app/helpers/e9_polls/polls_helper.rb', line 59

def link_to_destroy_nested_attribute
  link_to( t(:destroy_nested_attribute, :scope => :e9_polls), '#', :class => 'destroy-nested-association')
end

#nested_attribute_template(association_name, builder, options = {}) ⇒ Object



98
99
100
101
102
# File 'app/helpers/e9_polls/polls_helper.rb', line 98

def nested_attribute_template(association_name, builder, options = {})
  options.symbolize_keys!
  partial = options[:partial] || File.join('e9_polls', builder.object.class.model_name.collection, 'nested_attribute_template')
  render(:partial => partial, :locals => { :f => builder })
end

#nested_attribute_template_js(klass, association_name, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/e9_polls/polls_helper.rb', line 75

def nested_attribute_template_js(klass, association_name, options = {})
  options.symbolize_keys!
  options[:index] ||= 10000

  template = nil

  fields_for(klass.new) do |f|
    f.object.send(association_name).build
    f.fields_for(association_name, :child_index => options[:index]) do |ff|
      template = "#{escape_javascript(nested_attribute_template(association_name, ff))}"
    end
  end

  retv = <<-RETV
    TEMPLATES = window.TEMPLATES || {};
    TEMPLATES['#{association_name}'] = {
      rx: new RegExp(#{options[:index]}, 'g'),
      index: #{options[:index]},
      template: "#{template}"
    };
  RETV
end

Links



46
47
48
49
50
51
52
53
# File 'app/helpers/e9_polls/polls_helper.rb', line 46

def records_table_links_for_record(record)
  if respond_to?(method_name = "records_table_links_for_#{record.class.name.underscore}")
    send(method_name, record)
  else
    html_concat(
    )
  end
end

#render_nested_attribute_association(association_name, form, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/e9_polls/polls_helper.rb', line 63

def render_nested_attribute_association(association_name, form, options = {})
  options.symbolize_keys!

  association = resource.send(association_name)

  unless association.empty?
    form.fields_for(association_name) do |f|
      concat nested_attribute_template(association_name, f, options)
    end
  end
end