Class: Admin::Shop::Products::ImagesController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/admin/shop/products/images_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /admin/shop/products/1/images POST /admin/shop/products/1/images.js POST /admin/shop/products/1/images.json AJAX and HTML




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
# File 'app/controllers/admin/shop/products/images_controller.rb', line 70

def create
  notice = 'Successfully created Image.'
  error  = 'Unable to create Image.'
  
  begin
    @shop_product = ShopProduct.find(params[:product_id])
    
    if params[:image]
      @image = Image.create!(params[:image])
    elsif params[:attachment]
      @image = Image.find(params[:attachment][:image_id])
    end
    
    @attachment = Attachment.create!(:image => @image, :page => @shop_product.page)
    
    respond_to do |format|
      format.html {
        redirect_to edit_admin_shop_product_path(@shop_product)
      }
      format.js   { render :partial => '/admin/shop/products/edit/shared/image', :locals => { :image => @attachment } }
      format.json { render :json    => @attachment.to_json  }
    end
  rescue
    respond_to do |format|
      format.html { 
        flash[:error] = error
        redirect_to edit_admin_shop_product_path(@shop_product)
      }
      format.js   { render :text  => error, :status => :unprocessable_entity }
      format.json { render :json  => { :error => error }, :status => :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /admin/shop/products/1/images/1 DELETE /admin/shop/products/1/images/1.js DELETE /admin/shop/products/1/images/1.json AJAX and HTML




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
# File 'app/controllers/admin/shop/products/images_controller.rb', line 108

def destroy
  notice = 'Image deleted successfully.'
  error  = 'Unable to delete Image.'
  begin
    @shop_product = ShopProduct.find(params[:product_id])
    @image = @attachment.image
    @attachment.destroy
    
    respond_to do |format|
      format.html {
        redirect_to edit_admin_shop_product_path(@shop_product)
      }
      format.js   { render :partial => '/admin/shop/products/edit/shared/image', :locals => { :excerpt => @image } }
      format.json { render :json    => { :notice => notice }, :status => :ok }
    end
  rescue
    respond_to do |format|
      format.html {
        flash[:error] = error
        render :remove
      }
      format.js   { render :text    => error, :status => :unprocessable_entity }
      format.json { render :json    => { :error => error }, :status => :unprocessable_entity }
    end
  end
end

#indexObject

GET /admin/shop/products/1/images GET /admin/shop/products/1/images.js GET /admin/shop/products/1/images.json AJAX and HTML




9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/admin/shop/products/images_controller.rb', line 9

def index
  error = 'This Product has no Images.'
  
  @shop_product = ShopProduct.find(params[:product_id])
  
  unless @shop_product.images.empty?
    respond_to do |format|
      format.html { redirect_to edit_admin_shop_product_path(@shop_product) }
      format.js   { render :partial => '/admin/shop/products/edit/shared/image', :collection => @shop_product.images }
      format.json { render :json    => @shop_product.images.to_json }
    end
  else
    respond_to do |format|
      format.html { 
        flash[:error] = error
        redirect_to edit_admin_shop_product_path(@shop_product)
      }
      format.js   { render :text    => error, :status => :unprocessable_entity }
      format.json { render :json    => { :error => error }, :status => :unprocessable_entity }
    end
  end
end

#sortObject

PUT /admin/shop/products/1/images/sort PUT /admin/shop/products/1/images/sort.js PUT /admin/shop/products/1/images/sort.json AJAX and HTML




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
# File 'app/controllers/admin/shop/products/images_controller.rb', line 36

def sort
  notice = 'Successfully sorted Images.'
  error  = 'Could not sort Images.'
  begin
    @shop_product = ShopProduct.find(params[:product_id])
    
    @images = CGI::parse(params[:attachments])['product_attachments[]']
    @images.each_with_index do |id, index|
      Attachment.find(id).update_attributes!({ :position => index+1 })
    end
    
    respond_to do |format|
      format.html {
        redirect_to edit_admin_shop_product_path(@shop_product)
      }
      format.js   { render :partial => '/admin/shop/products/edit/shared/image', :collection => @shop_product.images }
      format.json { render :json    => @shop_product.images.to_json }
    end
  rescue
    respond_to do |format|
      format.html {
        flash[:error] = error
        redirect_to edit_admin_shop_product_path(@shop_product)
      }
      format.js   { render :text  => error, :status => :unprocessable_entity }
      format.json { render :json  => { :error => error }, :status => :unprocessable_entity }
    end
  end
end