Module: Gridion::GridHelper

Defined in:
lib/gridion/grid_helper.rb

Defined Under Namespace

Classes: GridBinding

Instance Method Summary collapse

Instance Method Details

#grid(collection, options = {}, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gridion/grid_helper.rb', line 36

def grid(collection, options={}, &block)
  grid_binding=GridBinding.new

  initialize_grid_binding(grid_binding)

  content=with_output_buffer {block.call(grid_binding) } if block_given?

  # we need to initialize the procs in the helper context otherwise other helpers inside the proc object (e.g. link_to) wont work

  
  options={:actions=>[:edit, :delete]}.merge(options).with_indifferent_access
  
  if %w(ol ul).include?(options[:table_tag])
    options[:table_header_tag]||="span"
    options[:table_row_tag]||="li"
    options[:table_cell_tag]||="span"
  end
  
  options[:table_tag]||="table"
  options[:table_header_tag]||="th"
  options[:table_header_wrapper_tag]="thead"
  options[:table_body_wrapper_tag]="tbody"
  options[:table_row_tag]||="tr"
  options[:table_cell_tag]||="td"
  options[:paginate]=true unless options.has_key?(:paginate)

  
  if collection.blank?
    if options[:render_empty_grid]
      grid_binding.header.call(options[:object_class], collection, options)
      grid_binding.filter.call(collection.first.class, collection, options) if grid_binding.filter.present?
      grid_binding.footer.call(options[:object_class], collection, options)
    end
  else
    grid_binding.header.call(collection.first.class, collection, options)
    grid_binding.filter.call(collection.first.class, collection, options) if grid_binding.filter.present?

    collection.each_with_index {|object, i| grid_binding.row.call(object.class, object, options.merge(:row_is_even=>i%2==1)) } #index starts from 0 
    
    if collection.respond_to?(:current_page) && defined?(Kaminari) # we assume only Kaminari is supported
      grid_binding.paginator.call(collection.first.class, collection, options) if collection.num_pages > 1 && options[:paginate]==true

    end
      
    
    grid_binding.footer.call(collection.first.class, collection, options)

  end

  return nil

end

#initialize_grid_binding(grid_binding) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/gridion/grid_helper.rb', line 88

def initialize_grid_binding(grid_binding)
  with_output_buffer do
    grid_binding.header do |klass, collection, options={}|
      table_tag = options[:table_tag]||"table"
      table_row_tag=options[:table_row_tag]||"tr"
      table_header_tag=options[:table_header_tag]||"th"
      table_header_wrapper_tag=options[:table_header_wrapper_tag]||"thead"
      table_body_wrapper_tag=options[:table_body_wrapper_tag]||"tbody"
      skip_field_class=options[:skip_field_class]||false
      field_class_prefix=options[:field_class_prefix]||""
      table_classes=[options[:class]].flatten.compact
      safe_concat("<#{table_tag} class=\"#{klass.name.downcase} #{table_classes.join(' ')}\">")
      
      columns=options[:columns]||klass.column_names
      safe_concat("<#{table_header_wrapper_tag}>")  if table_header_wrapper_tag.present?
      safe_concat("<#{table_row_tag} class=\"header\">")
      
      (columns).each do |col|
        col_label=klass.human_attribute_name(col)
        col_label = sort_link(options[:q], col, col_label) if defined?(:sort_link) && options.has_key?(:q) && options[:q].present?  
        safe_concat("<#{table_header_tag} class=\"header_cell #{field_class_prefix + '_' + col.to_s.parameterize.underscore unless skip_field_class}\">#{col_label}</#{table_header_tag}>")
      end
      safe_concat("<#{table_header_tag} class=\"actions\">#{I18n.t("gridion.headers.actions", "Actions")}</#{table_header_tag}>") unless options[:actions].blank?
      safe_concat("<#{table_header_tag} class=\"children\"></#{table_header_tag}>") if options.has_key?(:children)
      safe_concat("</#{table_row_tag}>")
      safe_concat("</#{table_header_wrapper_tag}>")  if table_header_wrapper_tag.present?
      safe_concat("<#{table_body_wrapper_tag}>")  if table_body_wrapper_tag.present?
    end

    grid_binding.row do  |klass, object, options={}|
      table_tag = options[:table_tag]||"table"
      table_row_tag=options[:table_row_tag]||"tr"
      table_cell_tag=options[:table_cell_tag]||"td"
      skip_field_class=options[:skip_field_class]||false
      field_class_prefix=options[:field_class_prefix]||""
      
      object_list=
        if options.has_key?(:parent)
          ([options[:parent]] + [object]).flatten 
        else
          [object]
        end

      if options.has_key?(:namespace)
        namespaces=options[:namespace]
        object_list = ([namespaces] + object_list).flatten
      end
        
      result =""
      aux_columns={}
      aux_columns=options[:aux_columns].with_indifferent_access if options.has_key?(:aux_columns)
      
      formats=(options[:formats]||{}).with_indifferent_access
      actions=options[:actions]
      columns=options[:columns]||klass.column_names

      row_id="#{klass.name}_#{object.id}"
      
      result << "<#{table_row_tag} id=\"#{row_id}\" data-id=\"#{object.id}\" class=\"#{options[:row_is_even] ? 'even' : 'odd'}\">"
      (columns).each do |col|
        if aux_columns[col].present?
          value=aux_columns[col].call(object, options)
        else
          value=object.send(col)
          if formats.has_key?(col)
        
            if formats[col]==:currency
              value=number_to_currency(value) 
            elsif formats[col].kind_of?(Hash)
              if formats[col].has_key?(:date)
                value=value.try(:to_date).try(:to_s, formats[col][:date])
              elsif formats[col].has_key?(:datetime)
                value=value.try(:to_s, formats[col][:datetime])
              end
            elsif formats[col].kind_of?(Proc)
              # use hook
              value=formats[col].call(value)
            end
          end
        end
        result << "<#{table_cell_tag} #{"class=\"#{field_class_prefix + '_' + col}\"" unless skip_field_class}>#{value}</#{table_cell_tag}>"
      end
        

      if actions.present?
        result << "<#{table_cell_tag} class=\"actions\">"
        if actions.kind_of?(Proc)
          result << actions.call(object, options)
        elsif actions.kind_of?(Array)
          result << "#{link_to I18n.t("gridion.actions.show", default: "Show"), object_list, :class=>%w{action_link show}}" if actions.include?(:show)
          result << "#{link_to I18n.t("gridion.actions.edit", default: "Edit"), [:edit]+ object_list, :class=>%w{action_link edit}}" if actions.include?(:edit)
          result << "#{link_to I18n.t("gridion.actions.delete", default: "Delete"), object_list, :class=>%w{action_link delete}, :method=>:delete, :confirm=>'Are you sure?'}"  if actions.include?(:delete)
        elsif actions.kind_of?(Hash)
          result << "#{link_to I18n.t("gridion.actions.show", default: "Show"), object_list, {:class=>%w{action_link show}}.merge(actions[:show]||{})}" if actions.has_key?(:show)
          result << "#{link_to I18n.t("gridion.actions.edit", default: "Edit"), [:edit]+ object_list, {:class=>%w{action_link edit}}.merge(actions[:edit]||{})}" if actions.include?(:edit)
          result << "#{link_to I18n.t("gridion.actions.delete", default: "Delete"), object_list, {:class=>%w{action_link delete}, :method=>:delete, :confirm=>'Are you sure?'}.merge(actions[:delete]||{})}"  if actions.include?(:delete)
        end
        result << "</#{table_cell_tag}>"
      end
      
      if options.has_key?(:children)
        result << "<#{table_cell_tag} class=\"children\">"
        children_hash=
          if options[:children].kind_of?(Array)
            options[:children].each_with_object({}) {|child, h| h[child]=child.to_s.singularize.classify.constantize.model_name.human.pluralize }
          else
            options[:children].keys.each_with_object({}) {|child, h| h[child]=options[:children][child]||child.to_s.singularize.classify.constantize.model_name.human.pluralize} 
          end

        children_hash.keys.each do |child|
          label= children_hash[child]
          label||= child.to_s.singularize.classify.constantize.model_name.human.pluralize
          result << link_to(label, [namespaces, object, child].flatten, :class=>"child_link #{child.to_s}")
        end
      
        result << "</#{table_cell_tag}>"
      end

      
      result << "</#{table_row_tag}>"
      safe_concat(result)

    end

    grid_binding.paginator do |klass, collection, options|
      table_tag = options[:table_tag]
      table_row_tag=options[:table_row_tag]
      table_cell_tag=options[:table_cell_tag]
      paginate_param_name=options[:paginate_param_name]||"page"
      
      colspans=(options[:columns]||klass.column_names).count + 1 + (options[:actions].blank? ? 0 : 1) #TODO: change this to number of action columns
      result = ""
      result << "<#{table_row_tag} class=\"paginator\">"
      result << "<#{table_cell_tag} colspan=\"#{colspans}\">"
      result << paginate(collection, param_name: paginate_param_name)
      result << "</#{table_cell_tag}>"
      result << "</#{table_row_tag}>"
      safe_concat(result)
    end

    grid_binding.footer do |klass, collection, options={}|
      table_tag = options[:table_tag]
      table_row_tag=options[:table_row_tag]
      table_cell_tag=options[:table_cell_tag]
      table_body_wrapper_tag=options[:table_body_wrapper_tag]
      safe_concat("</#{table_body_wrapper_tag}>")  if table_body_wrapper_tag.present?
      safe_concat("</#{table_tag}>")
    end
    

  end
end