Class: Lipsiadmin::Controller::Ext::ColumnStore
- Inherits:
-
Object
- Object
- Lipsiadmin::Controller::Ext::ColumnStore
- Defined in:
- lib/controller/ext.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#add(*args) ⇒ Object
Method for add columns to the Column Model.
-
#column_fields ⇒ Object
Return an array config for build an Ext.grid.ColumnModel() config.
-
#initialize(model) {|_self| ... } ⇒ ColumnStore
constructor
:nodoc.
-
#store_data(params, options = {}) ⇒ Object
Return a searched and paginated data collection for the ExtJS Ext.data.GroupingStore() json You can pass options like:.
-
#store_data_from(collection) ⇒ Object
Return data for a custom collection for the ExtJS Ext.data.GroupingStore() json.
-
#store_fields ⇒ Object
Return an array config for build an Ext.data.GroupingStore().
Constructor Details
#initialize(model) {|_self| ... } ⇒ ColumnStore
:nodoc
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
#data ⇒ Object (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) = { :method => args[0] } [:header] = args[1].is_a?(String) || args[1].is_a?(Symbol) ? args[1].to_s : nil args.each { |a| .merge!(a) if a.is_a?(Hash) } # Add some defaults [:header] ||= [:method].to_s [:sortable] = [:sortable].nil? ? true : [:sortable] # Try to translate header [:header] = @model.human_attribute_name([:header].to_s) # Reformat DataIndex if [:dataIndex].is_a?(Array) [:dataIndex] = [:dataIndex].collect do |f| f.is_a?(Symbol) ? "#{@model.table_name}.#{f}" : f end.join(",") end # Adding a name for our column [:name] ||= "#{@model.table_name.singularize}[#{[:method]}]" # Reformat query if [:method].is_a?(Symbol) [:dataIndex] ||= "#{@model.table_name}.#{[: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 = [:method].split(".") if columns.empty? [:dataIndex] ||= "#{@model.table_name}.#{[:method]}" else [:dataIndex] ||= columns[0..columns.length-2].collect(&:pluralize).join(".") + "." + columns.at(columns.size-1) end end # Reformat dataIndex [:mapping] ||= [:dataIndex].to_s.downcase.gsub(/[^a-z0-9]+/, '_'). gsub(/-+$/, '_'). gsub(/^-+$/, '_') # Now is necessary for our columns an ID [:id] = [:mapping] @data << end |
#column_fields ⇒ Object
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, ={}) # 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 => ) 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 |, data| [data[:mapping]] = (c.instance_eval(data[:method].to_s) rescue I18n.t("lipsiadmin.labels.not_found")) end store end end |
#store_fields ⇒ Object
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 |