Skip to content

Instantly share code, notes, and snippets.

@DAddYE
Created November 5, 2009 20:20
Show Gist options
  • Save DAddYE/227346 to your computer and use it in GitHub Desktop.
Save DAddYE/227346 to your computer and use it in GitHub Desktop.
# An advanced grid
#
# products_controller.rb
def show
params[:limit] ||= 50
# Our conditions
conditions = "products.store_id = #{current_store.id}"
conditions += " AND warehouses.id IN (#{current_account.warehouseship_ids.join(",")})" if current_account.magazziniere?
id = params[:id].gsub(/\D/, "") # Need to parse our ids
conditions += case params[:id].first
when "s" then " AND warehouses.supplier_id = #{id}" # Search for supplier
when "w" then " AND warehouses.id = #{id}" # Search for warehouse
when "c" then " AND categories.id = #{id}" # Search for category
when "e" then " AND ecommerces.id = #{id}" # Search for ecommerce
when "b" then " AND brand_id = #{id}" # Search for brand
when "v" then " AND active = #{id}" # Search for active/inactive
when "h" then " AND homepage = #{id}" # Search for products in home
when "o" then " AND special_price = #{id}" # Search for special price
when "t" then " AND hot = #{id}" # Search for hot
when "r" then " AND stick = #{id}" # Search for stick
when "d" then " AND recommended = #{id}" # Search for recommended
when "p" then " AND rating = #{id}" # Search for rating
else "" # All
end
@title = case params[:id].first
when "s" then "Supplier " + current_store.suppliers.find(id).name
when "w" then "Warehouse " + current_store.warehouses.find(id).name
when "c" then "Category " + current_store.categories.find(id).name
when "e" then "Ecommerce " + current_store.ecommerces.find(id).name
when "b" then "Brand" + current_store.brands.find(id).name
when "v" then (id == "1" ? "" : "Not") + " Active"
when "h" then (id == "1" ? "" : "Not") + " In Home Page"
when "o" then (id == "1" ? "" : "Not") + " In Special price"
when "t" then (id == "1" ? "" : "Not") + " Hot"
when "r" then (id == "1" ? "" : "Not") + " Stick"
when "d" then (id == "1" ? "" : "Not") + " Recommended"
when "p" then "Punteggio di #{id}"
else ""
end
# We made some alias for dont repeat this code
number_editor = { :xtype => :numberfield, :allowBlank => false }
number_positive_editor = number_editor.merge(:allowNegative => false)
text_editor = { :xtype => :textfield, :allowBlank => false }
checkbox_editor = { :xtype => :checkbox }
@column_store = column_store_for Product do |cm|
cm.add :custom_id
cm.add :name, :editor => text_editor
cm.add :code, :editor => text_editor
cm.add "brand.name", "Marca"
cm.add "warehouse.supplier.name", "Supplier", :dataIndex => ["suppliers.name"]
cm.add :correlations_count, :align => :center, :renderer => "correlations_renderer".to_l
cm.add :images_count, :align => :center, :renderer => "images_renderer".to_l
cm.add :files_count, :align => :center, :renderer => "files_renderer".to_l
cm.add :quantity, :editor => number_editor, :align => :center
cm.add :commercial_notes, "NC", :editor => text_editor, :hidden => true
if !current_account.warehouseman?
cm.add :vat, :editor => number_positive_editor, :align => :right, :renderer => :percentage
cm.add :price, :editor => number_positive_editor, :align => :right, :renderer => :eur_money
cm.add :cost, :editor => number_positive_editor, :align => :right, :renderer => :eur_money, :hidden => true
cm.add :discount, :editor => number_positive_editor, :align => :right, :renderer => :percentage
cm.add :price_discount, :align => :right, :renderer => :eur_money, :sortable => false
cm.add :rating, :editor => number_positive_editor, :align => :center
cm.add :on_request, :editor => checkbox_editor, :align => :center, :renderer => :boolean, :sortable => false
cm.add :homepage, :editor => checkbox_editor, :align => :center, :renderer => :boolean, :sortable => false
cm.add :active, :editor => checkbox_editor, :align => :center, :renderer => :boolean, :sortable => false
cm.add :recommended, :editor => checkbox_editor, :align => :center, :renderer => :boolean, :sortable => false
cm.add :hot, :editor => checkbox_editor, :align => :center, :renderer => :boolean, :sortable => false
cm.add :stick, :editor => checkbox_editor, :align => :center, :renderer => :boolean, :sortable => false
cm.add :special_price, :editor => checkbox_editor, :align => :center, :renderer => :boolean, :sortable => false
end
cm.add :date_expire, :editor => { :xtype => :datetimefield }, :align => :right, :renderer => :datetime, :hidden => true
cm.add :updated_at, :align => :right, :renderer => :datetime, :hidden => true
cm.add :created_at, :align => :right, :renderer => :datetime, :hidden => true
end
respond_to do |format|
format.js
format.json { render :json => @column_store.store_data(params) }
end
end
# This is a basic show.rjs
page << render(:partial => "renderers.js")
page.grid :editable => true do |grid|
grid.id "grid-products"
grid.title "Elenco Prodotto #{@title}"
grid.base_path "/backend/products"
grid.forgery_protection_token request_forgery_protection_token
grid.authenticity_token form_authenticity_token
grid.tbar :default
grid.sm :checkbox
grid.store do |store|
store.url "/backend/products/#{params[:id]}.json"
store.fields @column_store.store_fields
store.sortInfo :field => "products.created_at", :direction => "DESC"
end
grid.columns do |columns|
columns.fields @column_store.column_fields
end
grid.bbar :store => grid.get_store, :pageSize => params[:limit], :displayInfo => true
end
# This is our custom renderers
# for correlations_renderer images_renderer files_renderer
function images_renderer(v, p, r) {
return '<div style="cursor:pointer;color:#1B478E" '+
'onclick="var w = new Backend.window({ url: \'/backend/products/' + r.id +'/images\', form: true }); '+
'w.show(); return false">'+r.data['products.images_count']+
'</div>'
}
function files_renderer(v, p, r) {
return '<div style="cursor:pointer;color:#1B478E" '+
'onclick="var w = new Backend.window({ url: \'/backend/products/' + r.id +'/files\', form: true }); '+
'w.show(); return false">'+r.data['products.files_count']+
'</div>'
}
function correlations_renderer(v, p, r) {
return '<div style="cursor:pointer;color:#1B478E" '+
'onclick="var w = new Backend.window({ url: \'/backend/products/' + r.id +'/correlations\', width: 800, '+
'height:500 }); w.show(); return false">'+r.data['products.correlations_count']+
'</div>'
}
# And this is the popup for manage images
=edit_title_for(Product, "Images #{@product.name} (#{@product.code})")
.padding
-form_tag({:action => :update, :id => @product}, :method => :put, :multipart => true) do
=attachments_tag :product, :images, :order => true, :image => true
=hidden_field_tag :back_to, :images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment