Class: Caboose::MediaCategoriesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_edit, #admin_index, #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/media-categories



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/caboose/media_categories_controller.rb', line 36

def admin_add
  return unless user_is_allowed('mediacategories', 'add')

  resp = Caboose::StdClass.new
  
  cat = MediaCategory.new(
    :site_id   => @site.id,
    :parent_id => params[:parent_id],
    :name      => params[:name]
  )      
  if !cat.save
    resp.error = cat.errors.first[1]
  else
    resp.new_id = cat.id
    resp.refresh = true
  end
 
  render :json => resp
end

#admin_attachObject

POST /admin/media-categories/:id/attach



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/caboose/media_categories_controller.rb', line 92

def admin_attach
  return unless user_is_allowed('mediacategories', 'view')
  
  media_category_id = params[:id]
  ids = params[:media_id]
  ids = [ids] if !ids.is_a?(Array)
  ids.each do |id|
    m = Media.where(:id => id).first
    next if m.nil?
    m.update_attribute(:media_category_id, media_category_id)
    p = Product.where(:media_category_id => media_category_id).last
    if p
      pi = ProductImage.where(:media_id => id).exists? ? ProductImage.where(:media_id => id).first : ProductImage.create(:media_id => id, :product_id => p.id)
      pi.product_id = p.id
      pi.save
      ProductImageVariant.where(:product_image_id => pi.id).destroy_all
    end
  end
    
  render :json => { :success => true }
end

#admin_deleteObject

DELETE /admin/media-categories/:id



83
84
85
86
87
88
89
# File 'app/controllers/caboose/media_categories_controller.rb', line 83

def admin_delete
  return unless user_is_allowed('mediacategories', 'delete')
  cat = MediaCategory.find(params[:id])
  Media.where(:media_category_id => cat.id).destroy_all
  cat.destroy            
  render :json => { :success => true }
end

#admin_flat_treeObject

GET /admin/media-categories/flat-tree



13
14
15
16
17
18
# File 'app/controllers/caboose/media_categories_controller.rb', line 13

def admin_flat_tree
  return unless user_is_allowed('mediacategories', 'view')
  prefix = params[:prefix] ? params[:prefix] : '-  '
  tree = Caboose::MediaCategory.flat_tree(@site.id, prefix)
  render :json => tree
end

#admin_jsonObject

GET /admin/media-categories/json



6
7
8
9
10
# File 'app/controllers/caboose/media_categories_controller.rb', line 6

def admin_json
  return unless user_is_allowed('mediacategories', 'view')       
  tree = Caboose::MediaCategory.flat_tree(@site.id)
  render :json => tree
end

#admin_optionsObject

GET /admin/media-categories/options



21
22
23
24
25
26
27
# File 'app/controllers/caboose/media_categories_controller.rb', line 21

def admin_options
  return unless user_is_allowed('mediacategories', 'view')
  prefix = params[:prefix] ? params[:prefix] : '-  '
  tree = Caboose::MediaCategory.flat_tree(@site.id, prefix)
  options = tree.collect{ |mc| { 'value' => mc[:id], 'text' => mc[:name] }}
  render :json => options
end

#admin_treeObject

GET /admin/media-categories/tree



30
31
32
33
# File 'app/controllers/caboose/media_categories_controller.rb', line 30

def admin_tree
  return unless user_is_allowed('mediacategories', 'view')      
  render :json => Caboose::MediaCategory.tree_hash(@site.id)
end

#admin_updateObject

PUT /admin/media-categories/:id



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/caboose/media_categories_controller.rb', line 57

def admin_update
  return unless user_is_allowed('mediacategories', 'edit')
  
  resp = StdClass.new
  cat = MediaCategory.find(params[:id])
  
  save = true      
  params.each do |name, value|
    case name          
      when 'name' then cat.name = value
      when 'parent_id'
        parent = MediaCategory.find(value.to_i)
        if cat.is_ancestor_of?(parent)
          resp.error = "The new parent cannot be a child of itself."
          save = false
        else
          cat.parent_id = value
        end            
    end
  end

  resp.success = save && cat.save
  render :json => resp
end