Class: Admin::ResourcesController

Inherits:
BaseController show all
Includes:
Typus::Controller::Actions, Typus::Controller::Filters, Typus::Controller::Format, Typus::Controller::Headless
Defined in:
app/controllers/admin/resources_controller.rb

Constant Summary collapse

Whitelist =
[:edit, :update, :destroy, :toggle]

Instance Method Summary collapse

Methods included from Typus::Controller::Headless

#headless_mode?, included

Methods included from Typus::Controller::Filters

included

Instance Method Details

#createObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/admin/resources_controller.rb', line 45

def create
  # Note that we still can still assign the item to another model. To change
  # this behavior we need only to change how we merge the params.
  item_params = params[:resource] || {}
  item_params.merge!(params[@object_name])

  @item = @resource.new
  @item.assign_attributes(item_params, :as => current_role)

  set_attributes_on_create

  respond_to do |format|
    if @item.save
      format.html { redirect_on_success }
      format.json { render :json => @item, :status => :created, :location => @item }
    else
      format.html { render :action => "new" }
      format.json { render :json => @item.errors, :status => :unprocessable_entity }
    end
  end
end

#destroyObject



106
107
108
109
110
111
112
113
# File 'app/controllers/admin/resources_controller.rb', line 106

def destroy
  if @item.destroy
    notice = Typus::I18n.t("%{model} successfully removed.", :model => @resource.model_name.human)
  else
    alert = @item.errors.full_messages
  end
  redirect_to :back, :notice => notice, :alert => alert
end

#editObject



67
68
69
70
71
# File 'app/controllers/admin/resources_controller.rb', line 67

def edit
  custom_actions_for(:edit).each do |action|
    prepend_resources_action(action.titleize, {:action => action, :id => @item})
  end
end

#indexObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/admin/resources_controller.rb', line 16

def index
  get_objects

  custom_actions_for(:index).each do |action|
    prepend_resources_action(action.titleize, {:action => action, :id => nil})
  end

  respond_to do |format|
    format.html do
      set_default_action
      add_resource_action("Trash", {:action => "destroy"}, {:confirm => "#{Typus::I18n.t("Trash")}?", :method => 'delete'})
      generate_html
    end

    format.csv { generate_csv }
    format.json { export(:json) }
    format.xml { export(:xml) }
  end
end

#newObject



36
37
38
39
40
41
42
43
# File 'app/controllers/admin/resources_controller.rb', line 36

def new
  @item = @resource.new(params[:resource])

  respond_to do |format|
    format.html
    format.json { render :json => @item }
  end
end

#showObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/admin/resources_controller.rb', line 73

def show
  check_resource_ownership if @resource.typus_options_for(:only_user_items)

  if admin_user.can?('edit', @resource)
    prepend_resources_action("Edit", {:action => 'edit', :id => @item})
  end

  custom_actions_for(:show).each do |action|
    prepend_resources_action(action.titleize, {:action => action, :id => @item})
  end

  respond_to do |format|
    format.html
    format.xml { render :xml => @item }
    format.json { render :json => @item }
  end
end

#toggleObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/admin/resources_controller.rb', line 115

def toggle
  @item.toggle(params[:field])

  respond_to do |format|
    if @item.save
      format.html do
        notice = Typus::I18n.t("%{model} successfully updated.", :model => @resource.model_name.human)
        redirect_to :back, :notice => notice
      end
      format.json { render :json => @item }
    else
      format.html { render :edit }
      format.json { render :json => @item.errors, :status => :unprocessable_entity }
    end
  end
end

#updateObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/admin/resources_controller.rb', line 91

def update
  attributes = params[:_nullify] ? { params[:_nullify] => nil } : params[@object_name]

  respond_to do |format|
    if @item.update_attributes(attributes, :as => current_role)
      set_attributes_on_update
      format.html { redirect_on_success }
      format.json { render :json => @item }
    else
      format.html { render :edit }
      format.json { render :json => @item.errors, :status => :unprocessable_entity }
    end
  end
end