Skip to content

Commit

Permalink
Scaffold master_tune
Browse files Browse the repository at this point in the history
  • Loading branch information
ericjbowman committed Jun 7, 2019
1 parent dbfd544 commit 8c82d45
Show file tree
Hide file tree
Showing 21 changed files with 1,660 additions and 9 deletions.
51 changes: 51 additions & 0 deletions app/controllers/master_tunes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class MasterTunesController < ApplicationController
before_action :set_master_tune, only: [:show, :update, :destroy]

# GET /master_tunes
def index
@master_tunes = MasterTune.all

render json: @master_tunes
end

# GET /master_tunes/1
def show
render json: @master_tune
end

# POST /master_tunes
def create
@master_tune = MasterTune.new(master_tune_params)

if @master_tune.save
render json: @master_tune, status: :created, location: @master_tune
else
render json: @master_tune.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /master_tunes/1
def update
if @master_tune.update(master_tune_params)
render json: @master_tune
else
render json: @master_tune.errors, status: :unprocessable_entity
end
end

# DELETE /master_tunes/1
def destroy
@master_tune.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_master_tune
@master_tune = MasterTune.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def master_tune_params
params.require(:master_tune).permit(:title, :composer)
end
end
17 changes: 9 additions & 8 deletions app/controllers/tunes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TunesController < ApplicationController
class TunesController < OpenReadController
before_action :set_tune, only: [:show, :update, :destroy]

# GET /tunes
Expand All @@ -15,7 +15,7 @@ def show

# POST /tunes
def create
@tune = Tune.new(tune_params)
@tune = current_user.tunes.build(tune_params)

if @tune.save
render json: @tune, status: :created, location: @tune
Expand All @@ -39,13 +39,14 @@ def destroy
end

private

# Use callbacks to share common setup or constraints between actions.
def set_tune
@tune = Tune.find(params[:id])
end
def set_tune
@tune = current_user.tunes.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def tune_params
params.require(:tune).permit(:title, :composer)
end
def tune_params
params.require(:tune).permit(:title, :composer, :user_id)
end
end
2 changes: 2 additions & 0 deletions app/models/master_tune.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class MasterTune < ApplicationRecord
end
2 changes: 2 additions & 0 deletions app/models/tune.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Tune < ApplicationRecord
belongs_to :user
validates :user, presence: true
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
class User < ApplicationRecord
include Authentication
has_many :examples
has_many :tunes
end
3 changes: 3 additions & 0 deletions app/serializers/master_tune_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MasterTuneSerializer < ActiveModel::Serializer
attributes :id, :title, :composer
end
1 change: 1 addition & 0 deletions app/serializers/tune_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class TuneSerializer < ActiveModel::Serializer
attributes :id, :title, :composer
belongs_to :user
end
1 change: 1 addition & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

class UserSerializer < ActiveModel::Serializer
attributes :id, :email
has_many :tunes
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
resources :master_tunes
resources :tunes, except: %i[new edit]
# RESTful routes
resources :examples, except: %i[new edit]
Expand Down
13 changes: 13 additions & 0 deletions curl-scripts/tunes/create-tune.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
curl "http://localhost:4741/tunes" \
--include \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Token token=${TOKEN}" \
--data '{
"tune": {
"title": "'"${TITLE}"'",
"composer": "'"${COMPOSER}"'"
}
}'

echo
7 changes: 7 additions & 0 deletions curl-scripts/tunes/destroy-tune.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curl "http://localhost:4741/tunes/${ID}" \
--include \
--request DELETE \
# --header "Content-Type: application/json" \
# --header "Authorization: Token token=${TOKEN}" \

echo
5 changes: 5 additions & 0 deletions curl-scripts/tunes/show-tune.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl "http://localhost:4741/tunes/${ID}" \
--include \
--request GET \
--header "Content-Type: application/json" \
# --header "Authorization: Token token=${TOKEN}" \
13 changes: 13 additions & 0 deletions curl-scripts/tunes/update-tune.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
curl "http://localhost:4741/tunes/${ID}" \
--include \
--request PATCH \
--header "Content-Type: application/json" \
--header "Authorization: Token token=${TOKEN}" \
--data '{
"tune": {
"title": "'"${TITLE}"'",
"composer": "'"${COMPOSER}"'"
}
}'

echo
10 changes: 10 additions & 0 deletions db/migrate/20190607193250_create_master_tunes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateMasterTunes < ActiveRecord::Migration[5.2]
def change
create_table :master_tunes do |t|
t.string :title
t.string :composer

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_06_07_143547) do
ActiveRecord::Schema.define(version: 2019_06_07_193250) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -23,6 +23,13 @@
t.index ["user_id"], name: "index_examples_on_user_id"
end

create_table "master_tunes", force: :cascade do |t|
t.string "title"
t.string "composer"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "tunes", force: :cascade do |t|
t.string "title"
t.string "composer"
Expand Down
5 changes: 5 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)

require 'csv'

csv_text = File.read(Rails.root.join('lib', 'seeds', 'RepList.csv'))
puts csv_text
Loading

0 comments on commit 8c82d45

Please sign in to comment.