Module: Crudable::Rails::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/crudable/rails/base.rb

Constant Summary collapse

UNPROCESSABLE_STATUS =

Status code for unprocessable entity (422) Rails 7.1+ uses :unprocessable_content (replaces deprecated :unprocessable_entity)

:unprocessable_content

Instance Method Summary collapse

Instance Method Details

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crudable/rails/base.rb', line 41

def create
  unless skip_initialize_create?
    instance_variable_set("@#{resource_var_name}", new_instance)
    instance_variable_get("@#{resource_var_name}").assign_attributes(create_params)
  end
  resource = instance_variable_get("@#{resource_var_name}")
  # If skip_initialize_create? is true, resource might be nil - that's expected
  # Only return 404 if we tried to initialize but resource is still nil
  return render_not_found if resource.nil? && !skip_initialize_create?

  # Only authorize if resource exists (skip authorization when skip_initialize_create? is true)
  if authorize_with_pundit? && resource.present?
    before_authorize_create
    authorize_resource
    after_authorize_create
  elsif authorize_with_pundit?
    skip_authorization
  end
  if resource&.save
    on_successful_create
    on_successful_create_render
  else
    on_failed_create_setup
    on_failed_create_render
  end
rescue ActionController::ParameterMissing
  head :bad_request
end

#destroyObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/crudable/rails/base.rb', line 93

def destroy
  authorize_resource(destroy_method) if authorize_with_pundit?
  if instance_variable_get("@#{resource_var_name}").send(destroy_method)
    on_successful_destroy
    on_successful_destroy_render
  else
    on_failed_destroy
    on_failed_destroy_render
  end
end

#editObject



70
71
72
73
# File 'lib/crudable/rails/base.rb', line 70

def edit
  render_not_found if instance_variable_get("@#{resource_var_name}").blank?
  authorize_resource if authorize_with_pundit?
end

#indexObject



21
22
23
24
25
# File 'lib/crudable/rails/base.rb', line 21

def index
  authorize_class_action if authorize_with_pundit?
  instance_variable_set("@#{plural_resource_var_name}", resource_scope)
  paginate_resource
end

#newObject

Raises:

  • (ActiveRecord::RecordNotFound)


32
33
34
35
36
37
38
39
# File 'lib/crudable/rails/base.rb', line 32

def new
  instance = new_instance
  # Ensure we always set a valid instance to prevent nil model warnings in Rails 8
  raise ActiveRecord::RecordNotFound, "Could not create new #{resource_class.name}" if instance.nil?

  instance_variable_set("@#{resource_var_name}", instance)
  authorize_resource if authorize_with_pundit?
end

#showObject



27
28
29
30
# File 'lib/crudable/rails/base.rb', line 27

def show
  render_not_found if instance_variable_get("@#{resource_var_name}").blank?
  authorize_resource if authorize_with_pundit?
end

#updateObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/crudable/rails/base.rb', line 75

def update
  render_not_found if instance_variable_get("@#{resource_var_name}").blank?
  if authorize_with_pundit?
    before_authorize_update
    authorize_resource
    after_authorize_update
  end
  if instance_variable_get("@#{resource_var_name}").update update_params
    on_successful_update
    on_successful_update_render
  else
    on_failed_update_setup
    on_failed_update_render
  end
rescue ActionController::ParameterMissing
  head :bad_request
end