Class: Spree::Admin::ResourceController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/admin/resource_controller.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::ControllerHelpers::Common

included

Methods included from Core::ControllerHelpers::Auth

#current_ability, included, #redirect_back_or_default, #store_location, #try_spree_current_user, #unauthorized

Class Attribute Details

.callbacksObject

Returns the value of attribute callbacks.



92
93
94
# File 'app/controllers/spree/admin/resource_controller.rb', line 92

def callbacks
  @callbacks
end

.parent_dataObject

Returns the value of attribute parent_data.



91
92
93
# File 'app/controllers/spree/admin/resource_controller.rb', line 91

def parent_data
  @parent_data
end

Class Method Details

.belongs_to(model_name, options = {}) ⇒ Object



94
95
96
97
98
99
# File 'app/controllers/spree/admin/resource_controller.rb', line 94

def belongs_to(model_name, options = {})
  @parent_data ||= {}
  @parent_data[:model_name] = model_name
  @parent_data[:model_class] = model_name.to_s.classify.constantize
  @parent_data[:find_by] = options[:find_by] || :id
end

.createObject



106
107
108
109
# File 'app/controllers/spree/admin/resource_controller.rb', line 106

def create
  @callbacks ||= {}
  @callbacks[:create] ||= Spree::ActionCallbacks.new
end

.destroyObject



116
117
118
119
# File 'app/controllers/spree/admin/resource_controller.rb', line 116

def destroy
  @callbacks ||= {}
  @callbacks[:destroy] ||= Spree::ActionCallbacks.new
end

.new_actionObject



101
102
103
104
# File 'app/controllers/spree/admin/resource_controller.rb', line 101

def new_action
  @callbacks ||= {}
  @callbacks[:new_action] ||= Spree::ActionCallbacks.new
end

.updateObject



111
112
113
114
# File 'app/controllers/spree/admin/resource_controller.rb', line 111

def update
  @callbacks ||= {}
  @callbacks[:update] ||= Spree::ActionCallbacks.new
end

Instance Method Details

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/spree/admin/resource_controller.rb', line 41

def create
  invoke_callbacks(:create, :before)
  if @object.save
    invoke_callbacks(:create, :after)
    flash[:success] = flash_message_for(@object, :successfully_created)
    respond_with(@object) do |format|
      format.html { redirect_to location_after_save }
      format.js   { render :layout => false }
    end
  else
    invoke_callbacks(:create, :fails)
    respond_with(@object)
  end
end

#destroyObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/spree/admin/resource_controller.rb', line 66

def destroy
  invoke_callbacks(:destroy, :before)
  if @object.destroy
    invoke_callbacks(:destroy, :after)
    flash[:success] = flash_message_for(@object, :successfully_removed)
    respond_with(@object) do |format|
      format.html { redirect_to collection_url }
      format.js   { render :partial => "spree/admin/shared/destroy" }
    end
  else
    invoke_callbacks(:destroy, :fails)
    respond_with(@object) do |format|
      format.html { redirect_to collection_url }
    end
  end
end

#editObject



19
20
21
22
23
24
# File 'app/controllers/spree/admin/resource_controller.rb', line 19

def edit
  respond_with(@object) do |format|
    format.html { render :layout => !request.xhr? }
    format.js   { render :layout => false }
  end
end

#newObject



11
12
13
14
15
16
17
# File 'app/controllers/spree/admin/resource_controller.rb', line 11

def new
  invoke_callbacks(:new_action, :before)
  respond_with(@object) do |format|
    format.html { render :layout => !request.xhr? }
    format.js   { render :layout => false }
  end
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/spree/admin/resource_controller.rb', line 26

def update
  invoke_callbacks(:update, :before)
  if @object.update_attributes(params[object_name])
    invoke_callbacks(:update, :after)
    flash[:success] = flash_message_for(@object, :successfully_updated)
    respond_with(@object) do |format|
      format.html { redirect_to location_after_save }
      format.js   { render :layout => false }
    end
  else
    invoke_callbacks(:update, :fails)
    respond_with(@object)
  end
end

#update_positionsObject



56
57
58
59
60
61
62
63
64
# File 'app/controllers/spree/admin/resource_controller.rb', line 56

def update_positions
  params[:positions].each do |id, index|
    model_class.where(:id => id).update_all(:position => index)
  end

  respond_to do |format|
    format.js  { render :text => 'Ok' }
  end
end