Class: Jquard::ResourcesController

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

Instance Method Summary collapse

Instance Method Details

#createObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/jquard/resources_controller.rb', line 34

def create
  @page = @resource.page_for(:create)
  @form_schema = @resource.build_form

  data = @page.mutate_form_data_before_create(record_params.to_h)
  @record = @page.handle_record_creation(@resource.model, data)

  if @record.persisted?
    @page.after_create(@record)
    redirect_to @page.redirect_url_after_create(@record) || edit_resource_path(@resource.slug, @record.id),
      notice: @page.created_notification_message
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



70
71
72
73
# File 'app/controllers/jquard/resources_controller.rb', line 70

def destroy
  @record.destroy
  redirect_to resource_path(@resource.slug), notice: "#{@resource.singular_label} deleted"
end

#editObject



50
51
52
53
# File 'app/controllers/jquard/resources_controller.rb', line 50

def edit
  @page = @resource.page_for(:edit)
  @form_schema = @resource.build_form
end

#indexObject



16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/jquard/resources_controller.rb', line 16

def index
  @page = @resource.page_for(:index)
  @table = @resource.build_table
  @query_params = params.permit(:q, :sort, :direction, :page, :per_page)
  @result = Jquard::Tables::Query.new(
    table: @table,
    scope: @resource.model.all,
    params: @query_params
  ).result
end

#newObject



27
28
29
30
31
32
# File 'app/controllers/jquard/resources_controller.rb', line 27

def new
  @page = @resource.page_for(:create)
  @form_schema = @resource.build_form
  @record = @resource.model.new
  @form_schema.apply_defaults(@record)
end

#rootObject



6
7
8
9
10
11
12
13
14
# File 'app/controllers/jquard/resources_controller.rb', line 6

def root
  first = Jquard.registry.resources.min_by { |resource| [ resource.navigation_sort, resource.navigation_label ] }

  if first
    redirect_to resource_path(first.slug)
  else
    render plain: "No Jquard resources registered. Run `bin/rails generate jquard:resource YourModel`."
  end
end

#updateObject



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

def update
  @page = @resource.page_for(:edit)
  @form_schema = @resource.build_form

  data = @page.mutate_form_data_before_save(record_params.to_h)

  if @page.handle_record_update(@record, data)
    @page.after_save(@record)
    redirect_to @page.redirect_url_after_save(@record) || edit_resource_path(@resource.slug, @record.id),
      notice: @page.saved_notification_message
  else
    render :edit, status: :unprocessable_entity
  end
end