Module: Effective::CrudController

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/effective/crud_controller.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create ⇒ Object



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

def create
  self.resource ||= resource_class.new(send(resource_params_method_name))

  @page_title ||= "New #{resource_name.titleize}"
  EffectiveResources.authorized?(self, :create, resource)

  if resource.save
    flash[:success] = "Successfully created #{resource_name}"
    redirect_to(resource_redirect_path)
  else
    flash.now[:danger] = "Unable to create #{resource_name}: #{resource.errors.full_messages.to_sentence}"
    render :new
  end
end

#destroy ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/concerns/effective/crud_controller.rb', line 83

def destroy
  self.resource = resource_class.find(params[:id])

  @page_title = "Destroy #{resource}"
  EffectiveResources.authorized?(self, :destroy, resource)

  if resource.destroy
    flash[:success] = "Successfully deleted #{resource_name}"
  else
    flash[:danger] = "Unable to delete #{resource_name}: #{resource.errors.full_messages.to_sentence}"
  end

  request.referer.present? ? redirect_to(request.referer) : redirect_to(send(resource_index_path))
end

#edit ⇒ Object



61
62
63
64
65
66
# File 'app/controllers/concerns/effective/crud_controller.rb', line 61

def edit
  self.resource ||= resource_class.find(params[:id])

  @page_title ||= "Edit #{resource}"
  EffectiveResources.authorized?(self, :edit, resource)
end

#index ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/concerns/effective/crud_controller.rb', line 21

def index
  @page_title ||= resource_plural_name.titleize
  EffectiveResources.authorized?(self, :index, resource_class.new)

  self.resources ||= resource_class.all

  if resource_datatable_class
    @datatable ||= resource_datatable_class.new(resource_datatable_attributes, params[:scopes])
  end
end

#new ⇒ Object



32
33
34
35
36
37
# File 'app/controllers/concerns/effective/crud_controller.rb', line 32

def new
  self.resource ||= resource_class.new

  @page_title ||= "New #{resource_name.titleize}"
  EffectiveResources.authorized?(self, :new, resource)
end

#show ⇒ Object



54
55
56
57
58
59
# File 'app/controllers/concerns/effective/crud_controller.rb', line 54

def show
  self.resource ||= resource_class.find(params[:id])

  @page_title ||= resource.to_s
  EffectiveResources.authorized?(self, :show, resource)
end

#update ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/concerns/effective/crud_controller.rb', line 68

def update
  self.resource ||= resource_class.find(params[:id])

  @page_title = "Edit #{resource}"
  EffectiveResources.authorized?(self, :update, resource)

  if resource.update_attributes(send(resource_params_method_name))
    flash[:success] = "Successfully updated #{resource_name}"
    redirect_to(resource_redirect_path)
  else
    flash.now[:danger] = "Unable to update #{resource_name}: #{resource.errors.full_messages.to_sentence}"
    render :edit
  end
end