Class: Tenon::ResourcesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/tenon/resources_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ResourcesController

Returns a new instance of ResourcesController.



5
6
7
8
# File 'app/controllers/tenon/resources_controller.rb', line 5

def initialize(*args)
  self.class.load_and_authorize_resource(send(:load_options))
  super(*args)
end

Instance Method Details

#createObject



51
52
53
54
55
56
57
58
59
# File 'app/controllers/tenon/resources_controller.rb', line 51

def create
  self.resource = klass.new(resource_params).decorate
  authorize!(:publish, resource)
  if resource.save && !request.xhr?
    flash[:notice] = "#{human_name} saved successfully."
    save_item_version if resource.respond_to?(:versions)
  end
  respond_with(resource.decorate, location: after_create_path, status: (201 if resource.valid?))
end

#destroyObject



61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/tenon/resources_controller.rb', line 61

def destroy
  if resource.destroy
    respond_with(resource, location: polymorphic_index_path)
  else
    respond_to do |format|
      format.json do
        render json: { errors: resource.errors }
      end
    end
  end
end

#editObject



32
33
34
35
36
37
38
# File 'app/controllers/tenon/resources_controller.rb', line 32

def edit
  if params[:version] && resource.respond_to?(:revert)
    @item_version = Tenon::ItemVersion.find(params[:version])
    resource.revert(@item_version)
  end
  respond_with(resource)
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/tenon/resources_controller.rb', line 10

def index
  authorize!(:index, klass)
  params[:page] = 1 if params[:page].to_i == 0
  respond_to do |format|
    format.html
    format.json do
      self.collection = klass.all
      self.collection = collection.where(search_args) if params[:q]
      self.collection = collection.paginate(per_page: 20, page: params[:page])
      self.collection = Tenon::PaginatingDecorator.decorate(collection)
    end
  end
end

#newObject



24
25
26
27
28
29
30
# File 'app/controllers/tenon/resources_controller.rb', line 24

def new
  if params[:version] && resource.respond_to?(:revert)
    @item_version = Tenon::ItemVersion.find(params[:version])
    resource.revert(@item_version)
  end
  respond_with(resource)
end

#reorderObject



73
74
75
76
# File 'app/controllers/tenon/resources_controller.rb', line 73

def reorder
  self.collection = klass.reorder!(params['item_list'])
  respond_with(collection, location: polymorphic_index_path)
end

#updateObject



40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/tenon/resources_controller.rb', line 40

def update
  authorize!(:publish, resource)
  if resource.update_attributes(resource_params)
    save_item_version if resource.respond_to?(:versions)
    flash[:notice] = "#{human_name} saved successfully." unless request.xhr?
  end

  self.resource = resource.decorate
  respond_with(resource.decorate, location: after_update_path)
end