Class: Caboose::CategoriesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Caboose::CategoriesController
- Defined in:
- app/controllers/caboose/categories_controller.rb
Instance Method Summary collapse
-
#admin_add ⇒ Object
POST /admin/categories.
-
#admin_delete ⇒ Object
DELETE /admin/categories/:id.
-
#admin_edit ⇒ Object
GET /admin/categories/:id/edit.
-
#admin_index ⇒ Object
GET /admin/categories.
-
#admin_new ⇒ Object
GET /admin/categories/new.
-
#admin_options ⇒ Object
GET /admin/categories/options.
- #admin_options_helper(options, cat, prefix) ⇒ Object
-
#admin_status_options ⇒ Object
GET /admin/categories/status-options.
-
#admin_update ⇒ Object
PUT /admin/categories/:id.
Methods inherited from ApplicationController
#before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in
Instance Method Details
#admin_add ⇒ Object
POST /admin/categories
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 |
# File 'app/controllers/caboose/categories_controller.rb', line 21 def admin_add return unless user_is_allowed('categories', 'add') resp = Caboose::StdClass.new p = Category.where(:id => params[:parent_id]).first if params[:parent_id].nil? || params[:parent_id].empty? || p.nil? resp.error = 'Please select a parent category.' elsif params[:name].nil? || params[:name].empty? resp.error = 'This title cannot be empty' else cat = Category.new( :site_id => @site.id, :parent_id => p.id, :name => params[:name], :status => 'Active' ) cat.slug = cat.generate_slug cat.url = "#{p.url}/#{cat.slug}" if cat.save resp.redirect = "/admin/categories/#{cat.id}" else resp.error = 'There was an error saving the category.' end end render :json => resp end |
#admin_delete ⇒ Object
DELETE /admin/categories/:id
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/controllers/caboose/categories_controller.rb', line 89 def admin_delete return unless user_is_allowed('categories', 'delete') resp = Caboose::StdClass.new cat = Category.find(params[:id]) if cat.products.any? resp.error = "Can't delete a category that has products in it." elsif cat.children.any? resp.error = "You can't delete a category that has child categories." else resp.success = cat.destroy resp.redirect = '/admin/categories' end render :json => resp end |
#admin_edit ⇒ Object
GET /admin/categories/:id/edit
53 54 55 56 57 |
# File 'app/controllers/caboose/categories_controller.rb', line 53 def admin_edit return unless user_is_allowed('categories', 'edit') @category = Category.find(params[:id]) render :layout => 'caboose/admin' end |
#admin_index ⇒ Object
GET /admin/categories
9 10 11 12 |
# File 'app/controllers/caboose/categories_controller.rb', line 9 def admin_index return unless user_is_allowed('categories', 'view') render :layout => 'caboose/admin' end |
#admin_new ⇒ Object
GET /admin/categories/new
15 16 17 18 |
# File 'app/controllers/caboose/categories_controller.rb', line 15 def admin_new return unless user_is_allowed('categories', 'add') render :layout => 'caboose/admin' end |
#admin_options ⇒ Object
GET /admin/categories/options
116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/controllers/caboose/categories_controller.rb', line 116 def = [] cat = Category.where("site_id = ? and parent_id is null").first if cat.nil? cat = Category.create(:site_id => @site.id, :name => 'All Products', :url => '/') end cat.children.each do |c| (, c, '') end render :json => end |
#admin_options_helper(options, cat, prefix) ⇒ Object
128 129 130 131 132 133 |
# File 'app/controllers/caboose/categories_controller.rb', line 128 def (, cat, prefix) << { :value => cat.id, :text => "#{prefix}#{cat.name}" } cat.children.each do |c| (, c, "#{prefix} - ") end end |
#admin_status_options ⇒ Object
GET /admin/categories/status-options
107 108 109 110 111 112 113 |
# File 'app/controllers/caboose/categories_controller.rb', line 107 def render :json => [ { :value => 'Active' , :text => 'Active' }, { :value => 'Inactive' , :text => 'Inactive' }, { :value => 'Deleted' , :text => 'Deleted' } ] end |
#admin_update ⇒ Object
PUT /admin/categories/:id
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 |
# File 'app/controllers/caboose/categories_controller.rb', line 60 def admin_update return unless user_is_allowed('categories', 'edit') # Define category and initialize response cat = Category.find(params[:id]) resp = Caboose::StdClass.new({ :attributes => {} }) # Iterate over params and update relevant attributes params.each do |key, value| case key when 'site_id' then cat.name = value when 'name' then cat.name = value when 'slug' then cat.slug = value when 'status' then cat.status = value when 'image' then cat.image = value end end # Try and save category resp.success = cat.save # If an image is passed, return the url resp.attributes[:image] = { :value => cat.image.url(:medium) } if params[:image] # Respond to update request render :json => resp end |