Module: AlphaApi::Concerns::Actionable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/alpha_api/concerns/actionable.rb
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/alpha_api/concerns/actionable.rb', line 8 def create :create, resource_class new_resource = build_resource(permitted_create_params) if new_resource.valid? :create, new_resource new_resource.save render status: :created, json: resource_serializer.new(new_resource).serializable_hash else errors = reformat_validation_error(new_resource) raise Exceptions::ValidationErrors.new(errors), 'Validation Errors' end end |
#destroy ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/alpha_api/concerns/actionable.rb', line 57 def destroy if destroyable resource = resource_class.find(params[:id]) :destroy, resource if resource.destroy head :no_content else raise Exceptions::ValidationErrors.new(resource.errors), 'Validation Errors' end else raise Exceptions::MethodNotAllowed, 'Method Not Allowed' end end |
#index ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/alpha_api/concerns/actionable.rb', line 21 def index :read, resource_class query = apply_filter_and_sort(collection) apply_pagination if params[:page].present? records = paginate(query) records = records.padding(params[:page][:offset]) if params[:page][:offset] else records = query end = (nested_resources, params[:page], query.count) render json: resource_serializer.new(records, ).serializable_hash end |
#show ⇒ Object
36 37 38 39 40 41 |
# File 'lib/alpha_api/concerns/actionable.rb', line 36 def show resource = resource_class.find(params[:id]) :read, resource = (nested_resources) render json: resource_serializer.new(resource, ).serializable_hash end |
#update ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/alpha_api/concerns/actionable.rb', line 43 def update cached_resource_class = resource_class resource = cached_resource_class.find(params[:id]) :update, resource = (nested_resources) if resource.update(permitted_update_params(resource)) updated_resource = cached_resource_class.find(params[:id]) render json: resource_serializer.new(updated_resource, ).serializable_hash else errors = reformat_validation_error(resource) raise Exceptions::ValidationErrors.new(errors), 'Validation Errors' end end |