Module: IndexFor::Helper

Includes:
ShareHelper
Defined in:
lib/index_for/helper.rb

Instance Method Summary collapse

Instance Method Details

#index_for(objects, html_options = {}, &block) ⇒ Object

Creates a table around the objects and yields a builder.

Example:

index_for @users do |t|
  t.attribute :name
  t.attribute :email
end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/index_for/helper.rb', line 14

def index_for objects, html_options = {}, &block
  html_options = html_options.dup

  objects = fetch_objects objects, html_options

  tag = html_options[:table_tag] || IndexFor.table_tag
  klass = html_options[:klass] || objects.try(:klass) || objects.first.class

  html_options[:id] ||= index_for_id(klass)
  html_options[:class] = index_for_class(klass, html_options)

  head = index_for_head(klass.new, html_options, &block)
  body = index_for_body(objects, html_options, &block)
  content = head + body

  (tag, content, html_options)
end

#index_for_actions(object, *action_names, &block) ⇒ Object

Create action links and yields a builder.

Example:

index_for_actions @user do |a|
  a.action_link :show
  a.action_link :edit
end

index_for_actions @user, :show, :edit


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/index_for/helper.rb', line 60

def index_for_actions object, *action_names, &block
  html_options = action_names.extract_options!
  action_names = [:show, :edit, :destroy] if action_names == [:all]

  builder = html_options[:action_builder] || IndexFor::ActionBuilder
  builder = builder.new(object, html_options, self)

  content = capture(builder) do |a|
    action_names.map do |action_name|
      a.action_link action_name
    end.join.html_safe
  end

  content += capture(builder, &block) if block

  content
end

#show_for(object, html_options = {}, &block) ⇒ Object

Creates a desc list around the object and yields a builder.

Example:

show_for @user do |l|
  l.attribute :name
  l.attribute :email
end


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/index_for/helper.rb', line 87

def show_for object, html_options = {}, &block
  html_options = html_options.dup

  tag = html_options[:list_tag] || IndexFor.list_tag

  html_options[:id] ||= show_for_id(object)
  html_options[:class] = show_for_class(object, html_options)

  builder = html_options[:builder] || IndexFor::ListColumnBuilder
  content = capture(builder.new(object, html_options, self), &block)

  (tag, content, html_options)
end

#wice_index_for(objects, html_options = {}, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/index_for/helper.rb', line 32

def wice_index_for objects, html_options = {}, &block
  html_options = html_options.dup

  objects = fetch_objects objects, html_options

  builder = html_options[:builder] || IndexFor::WiceBuilder
  builder = builder.new(objects, html_options, self)
  block.call builder

  render partial: "index_for/wice_index_for", locals: {
    builder: builder,
    objects: builder.result,
    html_options: html_options,
    block: block
  }
end