Class: Caboose::CategoriesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/categories_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_json, #admin_json_single, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/categories



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
# File 'app/controllers/caboose/categories_controller.rb', line 26

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_category_productsObject

GET /admin/categories/:category_id/products/json



157
158
159
160
161
162
163
164
165
# File 'app/controllers/caboose/categories_controller.rb', line 157

def admin_category_products
  query = ["select P.id, P.title from store_category_memberships CM
    left join store_products P on P.id = CM.product_id
    where CM.category_id = ? AND P.status = 'Active'
    order by CM.sort_order, P.title", params[:id]]             
  rows = ActiveRecord::Base.connection.select_rows(ActiveRecord::Base.send(:sanitize_sql_array, query))
  arr = rows.collect{ |row| { :id => row[0], :title => row[1] }}
  render :json => arr
end

#admin_deleteObject

DELETE /admin/categories/:id



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/caboose/categories_controller.rb', line 112

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_editObject

GET /admin/categories/:id



58
59
60
61
62
# File 'app/controllers/caboose/categories_controller.rb', line 58

def admin_edit
  return unless user_is_allowed('categories', 'edit')    
  @category = Category.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_indexObject

GET /admin/categories



9
10
11
12
13
14
15
16
17
# File 'app/controllers/caboose/categories_controller.rb', line 9

def admin_index
  return unless user_is_allowed('categories', 'view')
  
  @top_cat = Caboose::Category.where("site_id = ? and parent_id is null", @site.id).first      
  if @top_cat.nil?
    @top_cat = Caboose::Category.create(:site_id => @site.id, :name => 'All Products', :url => '/')
  end
  render :layout => 'caboose/admin'
end

#admin_newObject

GET /admin/categories/new



20
21
22
23
# File 'app/controllers/caboose/categories_controller.rb', line 20

def admin_new
  return unless user_is_allowed('categories', 'add')
  render :layout => 'caboose/admin'
end

#admin_optionsObject

GET /admin/categories/options



139
140
141
142
143
144
145
146
147
# File 'app/controllers/caboose/categories_controller.rb', line 139

def admin_options
  @options = []
  cat = Category.where("site_id = ? and parent_id is null", @site.id).first      
  if cat.nil?
    cat = Category.create(:site_id => @site.id, :name => 'All Products', :url => '/')
  end
  admin_options_helper(cat, '')
  render :json => @options
end

#admin_options_helper(cat, prefix) ⇒ Object



149
150
151
152
153
154
# File 'app/controllers/caboose/categories_controller.rb', line 149

def admin_options_helper(cat, prefix)
  @options << { :value => cat.id, :text => "#{prefix}#{cat.name}" }      
  cat.children.each do |c|
    admin_options_helper(c, "#{prefix} - ")
  end      
end

#admin_sort_childrenObject

GET /admin/categories/:id/sort-children



65
66
67
68
69
# File 'app/controllers/caboose/categories_controller.rb', line 65

def admin_sort_children
  return unless user_is_allowed('categories', 'edit')    
  @category = Category.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_status_optionsObject

GET /admin/categories/status-options



130
131
132
133
134
135
136
# File 'app/controllers/caboose/categories_controller.rb', line 130

def admin_status_options
  render :json => [
    { :value => 'Active'   , :text => 'Active'    },
    { :value => 'Inactive' , :text => 'Inactive'  },
    { :value => 'Deleted'  , :text => 'Deleted'   }
  ]
end

#admin_updateObject

PUT /admin/categories/:id



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
# File 'app/controllers/caboose/categories_controller.rb', line 83

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

#admin_update_sort_orderObject

PUT /admin/categories/:id/children/sort-order



72
73
74
75
76
77
78
79
80
# File 'app/controllers/caboose/categories_controller.rb', line 72

def admin_update_sort_order
  return unless user_is_allowed('categories', 'edit')            
  params[:category_ids].each_with_index do |cat_id, i|
    cat = Category.find(cat_id)
    cat.sort_order = i
    cat.save
  end      
  render :json => { :success => true }
end