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
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
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 && resource.present? elsif 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 |
#destroy ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/crudable/rails/base.rb', line 93 def destroy (destroy_method) if 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 |
#edit ⇒ Object
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? if end |
#index ⇒ Object
21 22 23 24 25 |
# File 'lib/crudable/rails/base.rb', line 21 def index if instance_variable_set("@#{plural_resource_var_name}", resource_scope) paginate_resource end |
#new ⇒ Object
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) if end |
#show ⇒ Object
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? if end |
#update ⇒ Object
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 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 |