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



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

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_add_productObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/caboose/categories_controller.rb', line 170

def admin_add_product
  resp = Caboose::StdClass.new
  resp.success = false
  cat = Category.find(params[:category_id])
  prod = Product.find(params[:product_id])
  if cat && prod && cat.site_id == prod.site_id
    cm = CategoryMembership.where(:product_id => prod.id, :category_id => cat.id).first
    cm = CategoryMembership.create(:product_id => prod.id, :category_id => cat.id) if cm.nil?
    resp.success = true
  end
  render :json => resp
end

#admin_category_productsObject



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

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[:category_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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/caboose/categories_controller.rb', line 145

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



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

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

#admin_indexObject



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 => '/products', :slug => 'products', :status => 'Active')
  end
  render :layout => 'caboose/admin'
end

#admin_newObject



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



26
27
28
29
30
31
32
33
34
# File 'app/controllers/caboose/categories_controller.rb', line 26

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



162
163
164
165
166
167
# File 'app/controllers/caboose/categories_controller.rb', line 162

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



46
47
48
49
50
# File 'app/controllers/caboose/categories_controller.rb', line 46

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

#admin_status_optionsObject



37
38
39
40
41
42
43
# File 'app/controllers/caboose/categories_controller.rb', line 37

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

#admin_updateObject



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
# 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
      when 'parent_id' then cat.parent_id = 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



53
54
55
56
57
58
59
60
61
# File 'app/controllers/caboose/categories_controller.rb', line 53

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