Class: Lipsiadmin::Controller::Ext::ColumnStore

Inherits:
Object
  • Object
show all
Defined in:
lib/controller/ext.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) {|_self| ... } ⇒ ColumnStore

:nodoc

Yields:

  • (_self)

Yield Parameters:



59
60
61
62
63
# File 'lib/controller/ext.rb', line 59

def initialize(model, &block)#:nodoc
  @model = model
  @data = []
  yield self
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



57
58
59
# File 'lib/controller/ext.rb', line 57

def data
  @data
end

Instance Method Details

#add(*args) ⇒ Object

Method for add columns to the Column Model



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
# File 'lib/controller/ext.rb', line 66

def add(*args)
  options = { :method => args[0] }
  options[:header]     = args[1].is_a?(String) || args[1].is_a?(Symbol) ? args[1].to_s : nil
  
  args.each { |a| options.merge!(a) if a.is_a?(Hash)  }
  
  # Add some defaults
  options[:header]   ||= options[:method].to_s
  options[:sortable]   = options[:sortable].nil? ? true : options[:sortable]
  
  # Try to translate header
  options[:header] = @model.human_attribute_name(options[:header].to_s)
  
  # Reformat DataIndex
  if options[:dataIndex].is_a?(Array)
    options[:dataIndex] = options[:dataIndex].collect do |f| 
      f.is_a?(Symbol) ? "#{@model.table_name}.#{f}" : f 
    end.join(",")
  end
  
  # Adding a name for our column
  options[:name] ||= "#{@model.table_name.singularize}[#{options[:method]}]"
  
  # Reformat query
  if options[:method].is_a?(Symbol)
    options[:dataIndex] ||= "#{@model.table_name}.#{options[:method]}"
  else
    # if we have eg. prodcedure.category.name we need to build
    # a sql finder action so we need to generate
    # procedures.categories.name
    columns = options[:method].split(".")
    if columns.empty?
      options[:dataIndex] ||= "#{@model.table_name}.#{options[:method]}"
    else
      options[:dataIndex] ||= columns[0..columns.length-2].collect(&:pluralize).join(".") + "." + columns.at(columns.size-1)
    end
  end
  
  # Reformat dataIndex
  options[:mapping] ||= options[:dataIndex].to_s.downcase.gsub(/[^a-z0-9]+/, '_').
                                                          gsub(/-+$/, '_').
                                                          gsub(/^-+$/, '_')
  # Now is necessary for our columns an ID
  options[:id] = options[:mapping]
  
  @data << options
end

#column_fieldsObject

Return an array config for build an Ext.grid.ColumnModel() config



115
116
117
118
119
120
121
122
123
124
# File 'lib/controller/ext.rb', line 115

def column_fields
  @data.clone.inject([]) do |fields, data|
    # Prevent to removing in the original Hash
    field = data.clone
    field.delete(:method)
    field.delete(:mapping)
    fields << field
    fields
  end
end

#store_data(params, options = {}) ⇒ Object

Return a searched and paginated data collection for the ExtJS Ext.data.GroupingStore() json You can pass options like:

Examples

  store_data(params, :conditions => "found = 1")
  store_data(params, :include => :posts)


156
157
158
159
160
161
162
163
164
165
# File 'lib/controller/ext.rb', line 156

def store_data(params, options={})
  # Some can tell me that this method made two identical queries one for count one for paginate.
  # We don't use the select count because in some circumstances require much time than select *.
  @model.send(:with_scope, :find => options) do
    collection           = @model.ext_search(params)
    collection_count     = collection.length
    collection_paginated = collection.ext_paginate(params)
    { :results => store_data_from(collection_paginated), :count => collection_count }
  end
end

#store_data_from(collection) ⇒ Object

Return data for a custom collection for the ExtJS Ext.data.GroupingStore() json



138
139
140
141
142
143
144
145
146
# File 'lib/controller/ext.rb', line 138

def store_data_from(collection)
  collection.inject([]) do |store, c|
    store << @data.inject({ :id => c.id }) do |options, data|
      options[data[:mapping]] = (c.instance_eval(data[:method].to_s) rescue I18n.t("lipsiadmin.labels.not_found"))
      options
    end
    store
  end
end

#store_fieldsObject

Return an array config for build an Ext.data.GroupingStore()



127
128
129
130
131
132
133
134
135
# File 'lib/controller/ext.rb', line 127

def store_fields
  @data.inject([]) do |fields, data|
    hash = { :name => data[:dataIndex], :mapping => data[:mapping] }
    hash.merge!(:type => data[:renderer]) if  data[:renderer] && 
                                             (data[:renderer] == :date || data[:renderer] == :datetime || data[:renderer] == :time_to_date)
    fields << hash
    fields
  end
end