Class: Adminable::ResourcesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/adminable/resources_controller.rb

Instance Method Summary collapse

Constructor Details

#initializeResourcesController

Returns a new instance of ResourcesController.



3
4
5
6
7
8
9
# File 'app/controllers/adminable/resources_controller.rb', line 3

def initialize(*)
  @resource = Adminable::Configuration.resources.find do |resource|
    resource == Adminable::Resource.new(resource_model_name)
  end

  super
end

Instance Method Details

#createObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/adminable/resources_controller.rb', line 40

def create
  @entry = @resource.model.new(resource_params)

  if @entry.save
    redirect_to polymorphic_path(@resource.model),
                notice: t(
                  'adminable.resources.created',
                  resource: @resource.model.model_name.human
                )
  else
    flash.now[:alert] = @entry.errors.full_messages.first
    render :new
  end
end

#destroyObject



68
69
70
71
72
73
74
75
76
# File 'app/controllers/adminable/resources_controller.rb', line 68

def destroy
  @entry.destroy

  redirect_to polymorphic_path(@resource.model),
              notice: t(
                'adminable.resources.deleted',
                resource: @resource.model.model_name.human
              )
end

#editObject



36
37
# File 'app/controllers/adminable/resources_controller.rb', line 36

def edit
end

#indexObject



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

def index
  @q = @resource.model.ransack(params[:q])
  @entries = Adminable::Presenters::Entries.new(
    @q.result.includes(*includes).order(id: :desc)
                                          .page(params[:page]).per(25)
  )
end

#newObject



32
33
34
# File 'app/controllers/adminable/resources_controller.rb', line 32

def new
  @entry = @resource.model.new
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/adminable/resources_controller.rb', line 55

def update
  if @entry.update(resource_params)
    redirect_to polymorphic_path(@resource.model),
                notice: t(
                  'adminable.resources.updated',
                  resource: @resource.model.model_name.human
                )
  else
    flash.now[:alert] = @entry.errors.full_messages.first
    render :edit
  end
end