Class: Adminable::ResourcesController

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

Class Method Summary collapse

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

Class Method Details

.set_attributesObject

Calls from children controller class to manage resource attributes

Examples:

Update attributes for Adminable::Blog::PostsController

# app/controllers/adminable/blog/posts_controller.rb

set_attributes do |attributes|
  # Enables search for title column
  attributes.set :title, search: true

  # Hides title from new and edit pages
  attributes.set :title, form: true

  # Adds wysiwyg plugin and hides from index table
  attributes.set :text, wysiwyg: true, index: false

  # Adds new attribute `password` with type `string` and some options
  attributes.add :password, :string, wysiwyg: true, index: false

  # Adds new attribute `author`
  attributes << Adminable::Attributes::Types::String.new(:author)
end


97
98
99
100
101
# File 'app/controllers/adminable/resources_controller.rb', line 97

def self.set_attributes
  before_action do
    @resource.attributes.configure { yield(@resource.attributes) }
  end
end

Instance Method Details

#createObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



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

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



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

def destroy
  @entry.destroy

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

#editObject



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

def edit
end

#indexObject



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

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

#newObject



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

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

#updateObject



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

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