Class: Tenon::ResourcesController

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

Direct Known Subclasses

AssetsController, SimpleResourcesController

Instance Method Summary collapse

Instance Method Details

#createObject



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
111
112
# File 'app/controllers/tenon/resources_controller.rb', line 86

def create
  self.resource = klass.new(resource_params).decorate
  authorize(resource)
  before_create
  if resource.save
    flash[:notice] = "#{human_name} saved successfully." unless xhr_or_js?
    save_item_version if resource.respond_to?(:versions)
  end

  respond_to do |format|
    format.html do
      if resource.valid?
        redirect_to after_create_path
      else
        render action: :new
      end
    end

    format.json do
      if resource.valid?
        render json: resource
      else
        render status: 422, json: { errors: resource.errors }
      end
    end
  end
end

#destroyObject



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

def destroy
  self.resource = policy_scope(klass).find(params[:id])
  authorize(resource)
  before_destroy
  if resource.destroy
    respond_with(resource, location: polymorphic_index_path)
  else
    respond_to do |format|
      format.json do
        render status: 422, json: { errors: resource.errors }
      end
    end
  end
end

#editObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/tenon/resources_controller.rb', line 43

def edit
  self.resource = policy_scope(klass).find(params[:id])
  authorize(resource)
  before_edit
  if params[:version] && resource.respond_to?(:revert)
    @item_version = Tenon::ItemVersion.find(params[:version])
    resource.revert(@item_version)
  end

  respond_to do |format|
    format.html
  end
end

#indexObject



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

def index
  authorize(klass)
  params[:page] = 1 if params[:page].to_i == 0
  respond_to do |format|
    format.html
    format.json do
      self.collection = filterer.filter
      self.collection = collection.paginate(per_page: 50, page: params[:page])
      self.collection = Tenon::PaginatingDecorator.decorate(collection)
      respond_with(
        collection,
        serializer: Tenon::PaginatingSerializer,
        each_serializer: ActiveModel::Serializer.serializer_for(klass.new),
        root: 'records'
      )
    end
  end
end

#newObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/tenon/resources_controller.rb', line 29

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

  respond_to do |format|
    format.html
  end
end

#reorderObject



129
130
131
132
133
# File 'app/controllers/tenon/resources_controller.rb', line 129

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

#updateObject



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

def update
  self.resource = policy_scope(klass).find(params[:id])
  authorize(resource)
  before_update
  if resource.update_attributes(resource_params)
    save_item_version if resource.respond_to?(:versions)
    flash[:notice] = "#{human_name} saved successfully." unless xhr_or_js?
  end
  self.resource = resource.decorate

  respond_to do |format|
    format.html do
      if resource.valid?
        redirect_to after_update_path
      else
        render action: :edit
      end
    end

    format.json do
      if resource.valid?
        render json: resource
      else
        render status: 422, json: { errors: resource.errors }
      end
    end
  end
end