-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from ricardofelipe7/feature/implement-api-asse…
…mbly Feature/implement api assembly
- Loading branch information
Showing
10 changed files
with
119 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Api | ||
class AssembliesController < ApplicationController | ||
skip_before_action :verify_authenticity_token | ||
before_action :set_assembly, only: %i[show update destroy] | ||
def index | ||
@assemblies = Assembly.all | ||
render json: @assemblies, except: %i[created_at] | ||
end | ||
|
||
def show | ||
render json: @assembly | ||
end | ||
|
||
def create | ||
@assembly = Assembly.new(params_assembly) | ||
|
||
if @assembly.save | ||
render json: @assembly, status: :created, except: %i[created_at] | ||
else | ||
render json: @assembly.errors.full_messages, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if @assembly.update(params_assembly) | ||
render json: @assembly, except: %i[created_at] | ||
else | ||
render json: @assembly.errors.full_messages, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@assembly.destroy | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def set_assembly | ||
@assembly = Assembly.find(params[:id]) | ||
end | ||
|
||
def params_assembly | ||
params.require(:assembly).permit(:name, part_ids: [], book_ids: []) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,58 @@ | ||
class AssembliesController < ApplicationController | ||
before_action :set_assembly, only: %i[ show edit update destroy ] | ||
before_action :set_part_id_options, only: %i[ new show edit update destroy ] | ||
before_action :set_book_id_options, only: %i[ new show edit update destroy ] | ||
before_action :set_assembly, only: %i[show edit update destroy] | ||
before_action :set_part_id_options, only: %i[new show edit update destroy] | ||
before_action :set_book_id_options, only: %i[new show edit update destroy] | ||
|
||
# GET /assemblies or /assemblies.json | ||
def index | ||
@assemblies = Assembly.all | ||
end | ||
|
||
# GET /assemblies/1 or /assemblies/1.json | ||
def show | ||
end | ||
def show; end | ||
|
||
# GET /assemblies/new | ||
def new | ||
@assembly = Assembly.new | ||
end | ||
|
||
# GET /assemblies/1/edit | ||
def edit | ||
end | ||
def edit; end | ||
|
||
# POST /assemblies or /assemblies.json | ||
def create | ||
@assembly = Assembly.new(assembly_params) | ||
|
||
respond_to do |format| | ||
if @assembly.save | ||
format.html { redirect_to assembly_url(@assembly), notice: "Assembly was successfully created." } | ||
format.json { render :show, status: :created, location: @assembly } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @assembly.errors, status: :unprocessable_entity } | ||
end | ||
if @assembly.save | ||
redirect_to assembly_url(@assembly), notice: 'Assembly was successfully created.' | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /assemblies/1 or /assemblies/1.json | ||
def update | ||
respond_to do |format| | ||
if @assembly.update(assembly_params) | ||
format.html { redirect_to assembly_url(@assembly), notice: "Assembly was successfully updated." } | ||
format.json { render :show, status: :ok, location: @assembly } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @assembly.errors, status: :unprocessable_entity } | ||
end | ||
if @assembly.update(assembly_params) | ||
redirect_to assembly_url(@assembly), notice: 'Assembly was successfully updated.' | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /assemblies/1 or /assemblies/1.json | ||
def destroy | ||
@assembly.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to assemblies_url, notice: "Assembly was successfully destroyed." } | ||
format.json { head :no_content } | ||
end | ||
redirect_to assemblies_url, notice: 'Assembly was successfully destroyed.' | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_book_id_options | ||
@book_id_options = Book.all.pluck(:published_at, :id) | ||
end | ||
def set_part_id_options | ||
@part_id_options = Part.all.pluck(:part_number, :id) | ||
end | ||
def set_assembly | ||
@assembly = Assembly.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def assembly_params | ||
params.require(:assembly).permit(:name, :part_id, :book_id) | ||
end | ||
def set_book_id_options | ||
@book_id_options = Book.all.pluck(:published_at, :id) | ||
end | ||
|
||
def set_part_id_options | ||
@part_id_options = Part.all.pluck(:part_number, :id) | ||
end | ||
|
||
def set_assembly | ||
@assembly = Assembly.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def assembly_params | ||
params.require(:assembly).permit(:name, :part_id, :book_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module Api::AssembliesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
class Assembly < ApplicationRecord | ||
has_many :part, through: :assembly_parts | ||
belongs_to :book | ||
has_many :assembly_parts | ||
has_and_belongs_to_many :part | ||
has_one :book | ||
|
||
validates :name, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ | |
resources :suppliers | ||
resources :accounts | ||
resources :parts | ||
resources :assemblies | ||
end | ||
end |
4 changes: 2 additions & 2 deletions
4
...e/20230714202258_create_assembly_parts.rb → ...20230714202258_create_assemblies_parts.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Api::Assemblies", type: :request do | ||
describe "GET /index" do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
end | ||
end |