Lipsiasoft (owner)

Revisions

gist: 227346 Download_button fork
public
Public Clone URL: git://gist.github.com/227346.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
# 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