Add project scaffold
This commit is contained in:
parent
4b11641834
commit
bd385d8f0b
3
app/assets/javascripts/projects.coffee
Normal file
3
app/assets/javascripts/projects.coffee
Normal file
@ -0,0 +1,3 @@
|
||||
# Place all the behaviors and hooks related to the matching controller here.
|
||||
# All this logic will automatically be available in application.js.
|
||||
# You can use CoffeeScript in this file: http://coffeescript.org/
|
||||
3
app/assets/stylesheets/projects.scss
Normal file
3
app/assets/stylesheets/projects.scss
Normal file
@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the Projects controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: https://sass-lang.com/
|
||||
@ -22,7 +22,8 @@ class ApplicationController < ActionController::Base
|
||||
def user_not_authorized(exception)
|
||||
policy_name = exception.policy.class.to_s.underscore
|
||||
# flash[:alert] = "#{policy_name}.#{exception.query}"
|
||||
flash[:alert] = "Unable to #{exception.query[0..-2]} #{policy_name.split("_")[0].capitalize}"
|
||||
# flash[:alert] = "Unable to #{exception.query[0..-2]} #{policy_name.split("_")[0].capitalize}"
|
||||
flash[:error] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default
|
||||
redirect_to (request.referrer || root_path)
|
||||
end
|
||||
end
|
||||
|
||||
72
app/controllers/projects_controller.rb
Normal file
72
app/controllers/projects_controller.rb
Normal file
@ -0,0 +1,72 @@
|
||||
class ProjectsController < ApplicationController
|
||||
before_action :set_project, only: %i[ show edit update destroy ]
|
||||
|
||||
# GET /projects or /projects.json
|
||||
def index
|
||||
@projects = authorize Project.all
|
||||
end
|
||||
|
||||
# GET /projects/1 or /projects/1.json
|
||||
def show
|
||||
authorize @project
|
||||
end
|
||||
|
||||
# GET /projects/new
|
||||
def new
|
||||
authorize @project = current_user.projects.new
|
||||
end
|
||||
|
||||
# GET /projects/1/edit
|
||||
def edit
|
||||
authorize @project
|
||||
end
|
||||
|
||||
# POST /projects or /projects.json
|
||||
def create
|
||||
authorize @project = Project.new(project_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @project.save
|
||||
format.html { redirect_to project_url(@project), notice: "Project was successfully created." }
|
||||
format.json { render :show, status: :created, location: @project }
|
||||
else
|
||||
format.html { render :new, status: :unprocessable_entity }
|
||||
format.json { render json: @project.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /projects/1 or /projects/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @project.update(project_params)
|
||||
format.html { redirect_to project_url(@project), notice: "Project was successfully updated." }
|
||||
format.json { render :show, status: :ok, location: @project }
|
||||
else
|
||||
format.html { render :edit, status: :unprocessable_entity }
|
||||
format.json { render json: @project.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /projects/1 or /projects/1.json
|
||||
def destroy
|
||||
@project.destroy
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to projects_url, notice: "Project was successfully destroyed." }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_project
|
||||
@project = Project.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def project_params
|
||||
params.require(:project).permit(:title, :description, :user_id)
|
||||
end
|
||||
end
|
||||
2
app/helpers/projects_helper.rb
Normal file
2
app/helpers/projects_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module ProjectsHelper
|
||||
end
|
||||
3
app/models/project.rb
Normal file
3
app/models/project.rb
Normal file
@ -0,0 +1,3 @@
|
||||
class Project < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
end
|
||||
@ -5,6 +5,7 @@ class User < ActiveRecord::Base
|
||||
validates :soft_token, presence: true, uniqueness: true
|
||||
has_many :canisters
|
||||
has_many :doodads
|
||||
has_many :projects
|
||||
|
||||
def icid
|
||||
stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize
|
||||
|
||||
56
app/policies/project_policy.rb
Normal file
56
app/policies/project_policy.rb
Normal file
@ -0,0 +1,56 @@
|
||||
class ProjectPolicy
|
||||
attr_reader :user, :project
|
||||
|
||||
def initialize(user, project)
|
||||
@user = user || User.new
|
||||
@project = project
|
||||
end
|
||||
|
||||
class Scope
|
||||
def initialize(user, scope)
|
||||
@user = user
|
||||
@scope = scope
|
||||
end
|
||||
|
||||
def resolve
|
||||
if user.admin?
|
||||
scope.all
|
||||
else
|
||||
scope.all
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :user, :scope
|
||||
end
|
||||
|
||||
def index?
|
||||
true
|
||||
end
|
||||
|
||||
def show?
|
||||
true
|
||||
end
|
||||
|
||||
def create?
|
||||
@user.admin? || @user.signed_in?
|
||||
end
|
||||
|
||||
def new?
|
||||
@user.signed_in?
|
||||
end
|
||||
|
||||
def edit?
|
||||
@user.admin? || @user == @project.user
|
||||
end
|
||||
|
||||
def update?
|
||||
@user.admin? || @user == @project.user
|
||||
end
|
||||
|
||||
def destroy?
|
||||
@user.admin? || @user == @project.user
|
||||
end
|
||||
|
||||
end
|
||||
@ -1,6 +1,7 @@
|
||||
<nav>
|
||||
<ul>
|
||||
<li><%= link_to 'Home', root_path, data: { turbo: false } %></li>
|
||||
<li><%= link_to 'Projects', projects_path %></li>
|
||||
<li><%= link_to 'Doodads', doodads_path %></li>
|
||||
<% if !request.base_url.match(/pin|my|fireside/) %>
|
||||
<% if policy(:table).index? %>
|
||||
|
||||
10
app/views/projects/_form.html.haml
Normal file
10
app/views/projects/_form.html.haml
Normal file
@ -0,0 +1,10 @@
|
||||
= simple_form_for(@project) do |f|
|
||||
= f.error_notification
|
||||
|
||||
.form-inputs
|
||||
= f.input :title
|
||||
= f.input :description
|
||||
= f.hidden_field :user_id
|
||||
|
||||
.form-actions
|
||||
= f.button :submit
|
||||
2
app/views/projects/_project.json.jbuilder
Normal file
2
app/views/projects/_project.json.jbuilder
Normal file
@ -0,0 +1,2 @@
|
||||
json.extract! project, :id, :title, :description, :user_id, :created_at, :updated_at
|
||||
json.url project_url(project, format: :json)
|
||||
7
app/views/projects/edit.html.haml
Normal file
7
app/views/projects/edit.html.haml
Normal file
@ -0,0 +1,7 @@
|
||||
%h1 Editing project
|
||||
|
||||
= render 'form'
|
||||
|
||||
= link_to 'Show', @project
|
||||
\|
|
||||
= link_to 'Back', projects_path
|
||||
31
app/views/projects/index.html.haml
Normal file
31
app/views/projects/index.html.haml
Normal file
@ -0,0 +1,31 @@
|
||||
%h1 Listing projects
|
||||
|
||||
-# - if policy(Project).new?
|
||||
= link_to '+ Add New Project', new_project_path
|
||||
|
||||
%table
|
||||
%thead
|
||||
%tr
|
||||
%th Title
|
||||
%th Description
|
||||
%th Owner
|
||||
%th
|
||||
%th
|
||||
%th
|
||||
|
||||
%tbody
|
||||
- @projects.each do |project|
|
||||
%tr
|
||||
%td= link_to project.title, project
|
||||
%td= project.description
|
||||
%td= project.user
|
||||
-# %td= link_to 'Show', project
|
||||
%td
|
||||
- if policy(project).edit?
|
||||
%td= link_to 'Edit', edit_project_path(project)
|
||||
- else
|
||||
%td
|
||||
- if policy(project).destroy?
|
||||
%td= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' }
|
||||
- else
|
||||
%td
|
||||
1
app/views/projects/index.json.jbuilder
Normal file
1
app/views/projects/index.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.array! @projects, partial: "projects/project", as: :project
|
||||
5
app/views/projects/new.html.haml
Normal file
5
app/views/projects/new.html.haml
Normal file
@ -0,0 +1,5 @@
|
||||
%h1 New project
|
||||
|
||||
= render 'form'
|
||||
|
||||
= link_to 'Back', projects_path
|
||||
13
app/views/projects/show.html.haml
Normal file
13
app/views/projects/show.html.haml
Normal file
@ -0,0 +1,13 @@
|
||||
- if policy(@project).edit?
|
||||
= link_to 'Edit', edit_project_path(@project)
|
||||
|
||||
%p
|
||||
%b Title:
|
||||
= @project.title
|
||||
%p
|
||||
%b Description:
|
||||
= @project.description
|
||||
%p
|
||||
%b Owner:
|
||||
= @project.user
|
||||
|
||||
1
app/views/projects/show.json.jbuilder
Normal file
1
app/views/projects/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! "projects/project", project: @project
|
||||
8
config/locales/pundit.en.yml
Normal file
8
config/locales/pundit.en.yml
Normal file
@ -0,0 +1,8 @@
|
||||
en:
|
||||
pundit:
|
||||
default: 'You cannot perform this action.'
|
||||
project_policy:
|
||||
new?: 'You must sign in to create a new Project!'
|
||||
post_policy:
|
||||
update?: 'You cannot edit this post!'
|
||||
create?: 'You cannot create posts!'
|
||||
@ -1,4 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :projects
|
||||
resources :doodads
|
||||
resources :canisters
|
||||
resources :players
|
||||
|
||||
11
db/migrate/20231026005659_create_projects.rb
Normal file
11
db/migrate/20231026005659_create_projects.rb
Normal file
@ -0,0 +1,11 @@
|
||||
class CreateProjects < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :projects do |t|
|
||||
t.string :title
|
||||
t.text :description
|
||||
t.belongs_to :user, null: false, foreign_key: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
12
db/schema.rb
12
db/schema.rb
@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2023_10_15_004113) do
|
||||
ActiveRecord::Schema.define(version: 2023_10_26_005659) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@ -96,6 +96,15 @@ ActiveRecord::Schema.define(version: 2023_10_15_004113) do
|
||||
t.index ["player_id"], name: "index_plays_on_player_id"
|
||||
end
|
||||
|
||||
create_table "projects", force: :cascade do |t|
|
||||
t.string "title"
|
||||
t.text "description"
|
||||
t.bigint "user_id", null: false
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.index ["user_id"], name: "index_projects_on_user_id"
|
||||
end
|
||||
|
||||
create_table "seats", force: :cascade do |t|
|
||||
t.integer "table_id"
|
||||
t.integer "user_id"
|
||||
@ -162,4 +171,5 @@ ActiveRecord::Schema.define(version: 2023_10_15_004113) do
|
||||
|
||||
add_foreign_key "canisters", "users"
|
||||
add_foreign_key "doodads", "users"
|
||||
add_foreign_key "projects", "users"
|
||||
end
|
||||
|
||||
7
spec/factories/projects.rb
Normal file
7
spec/factories/projects.rb
Normal file
@ -0,0 +1,7 @@
|
||||
FactoryGirl.define do
|
||||
factory :project do
|
||||
title "MyString"
|
||||
description "MyText"
|
||||
user nil
|
||||
end
|
||||
end
|
||||
15
spec/helpers/projects_helper_spec.rb
Normal file
15
spec/helpers/projects_helper_spec.rb
Normal file
@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the ProjectsHelper. For example:
|
||||
#
|
||||
# describe ProjectsHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe ProjectsHelper, type: :helper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
5
spec/models/project_spec.rb
Normal file
5
spec/models/project_spec.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Project, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
135
spec/requests/projects_spec.rb
Normal file
135
spec/requests/projects_spec.rb
Normal file
@ -0,0 +1,135 @@
|
||||
require 'rails_helper'
|
||||
|
||||
# This spec was generated by rspec-rails when you ran the scaffold generator.
|
||||
# It demonstrates how one might use RSpec to test the controller code that
|
||||
# was generated by Rails when you ran the scaffold generator.
|
||||
#
|
||||
# It assumes that the implementation code is generated by the rails scaffold
|
||||
# generator. If you are using any extension libraries to generate different
|
||||
# controller code, this generated spec may or may not pass.
|
||||
#
|
||||
# It only uses APIs available in rails and/or rspec-rails. There are a number
|
||||
# of tools you can use to make these specs even more expressive, but we're
|
||||
# sticking to rails and rspec-rails APIs to keep things simple and stable.
|
||||
|
||||
RSpec.describe "/projects", type: :request do
|
||||
|
||||
# This should return the minimal set of attributes required to create a valid
|
||||
# Project. As you add validations to Project, be sure to
|
||||
# adjust the attributes here as well.
|
||||
let(:valid_attributes) {
|
||||
skip("Add a hash of attributes valid for your model")
|
||||
}
|
||||
|
||||
let(:invalid_attributes) {
|
||||
skip("Add a hash of attributes invalid for your model")
|
||||
}
|
||||
|
||||
describe "GET /index" do
|
||||
it "renders a successful response" do
|
||||
Project.create! valid_attributes
|
||||
get projects_url
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /show" do
|
||||
it "renders a successful response" do
|
||||
project = Project.create! valid_attributes
|
||||
get project_url(project)
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /new" do
|
||||
it "renders a successful response" do
|
||||
get new_project_url
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /edit" do
|
||||
it "renders a successful response" do
|
||||
project = Project.create! valid_attributes
|
||||
get edit_project_url(project)
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /create" do
|
||||
context "with valid parameters" do
|
||||
it "creates a new Project" do
|
||||
expect {
|
||||
post projects_url, params: { project: valid_attributes }
|
||||
}.to change(Project, :count).by(1)
|
||||
end
|
||||
|
||||
it "redirects to the created project" do
|
||||
post projects_url, params: { project: valid_attributes }
|
||||
expect(response).to redirect_to(project_url(Project.last))
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid parameters" do
|
||||
it "does not create a new Project" do
|
||||
expect {
|
||||
post projects_url, params: { project: invalid_attributes }
|
||||
}.to change(Project, :count).by(0)
|
||||
end
|
||||
|
||||
|
||||
it "renders a successful response (i.e. to display the 'new' template)" do
|
||||
post projects_url, params: { project: invalid_attributes }
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe "PATCH /update" do
|
||||
context "with valid parameters" do
|
||||
let(:new_attributes) {
|
||||
skip("Add a hash of attributes valid for your model")
|
||||
}
|
||||
|
||||
it "updates the requested project" do
|
||||
project = Project.create! valid_attributes
|
||||
patch project_url(project), params: { project: new_attributes }
|
||||
project.reload
|
||||
skip("Add assertions for updated state")
|
||||
end
|
||||
|
||||
it "redirects to the project" do
|
||||
project = Project.create! valid_attributes
|
||||
patch project_url(project), params: { project: new_attributes }
|
||||
project.reload
|
||||
expect(response).to redirect_to(project_url(project))
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid parameters" do
|
||||
|
||||
it "renders a successful response (i.e. to display the 'edit' template)" do
|
||||
project = Project.create! valid_attributes
|
||||
patch project_url(project), params: { project: invalid_attributes }
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /destroy" do
|
||||
it "destroys the requested project" do
|
||||
project = Project.create! valid_attributes
|
||||
expect {
|
||||
delete project_url(project)
|
||||
}.to change(Project, :count).by(-1)
|
||||
end
|
||||
|
||||
it "redirects to the projects list" do
|
||||
project = Project.create! valid_attributes
|
||||
delete project_url(project)
|
||||
expect(response).to redirect_to(projects_url)
|
||||
end
|
||||
end
|
||||
end
|
||||
38
spec/routing/projects_routing_spec.rb
Normal file
38
spec/routing/projects_routing_spec.rb
Normal file
@ -0,0 +1,38 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe ProjectsController, type: :routing do
|
||||
describe "routing" do
|
||||
it "routes to #index" do
|
||||
expect(get: "/projects").to route_to("projects#index")
|
||||
end
|
||||
|
||||
it "routes to #new" do
|
||||
expect(get: "/projects/new").to route_to("projects#new")
|
||||
end
|
||||
|
||||
it "routes to #show" do
|
||||
expect(get: "/projects/1").to route_to("projects#show", id: "1")
|
||||
end
|
||||
|
||||
it "routes to #edit" do
|
||||
expect(get: "/projects/1/edit").to route_to("projects#edit", id: "1")
|
||||
end
|
||||
|
||||
|
||||
it "routes to #create" do
|
||||
expect(post: "/projects").to route_to("projects#create")
|
||||
end
|
||||
|
||||
it "routes to #update via PUT" do
|
||||
expect(put: "/projects/1").to route_to("projects#update", id: "1")
|
||||
end
|
||||
|
||||
it "routes to #update via PATCH" do
|
||||
expect(patch: "/projects/1").to route_to("projects#update", id: "1")
|
||||
end
|
||||
|
||||
it "routes to #destroy" do
|
||||
expect(delete: "/projects/1").to route_to("projects#destroy", id: "1")
|
||||
end
|
||||
end
|
||||
end
|
||||
28
spec/views/projects/edit.html.haml_spec.rb
Normal file
28
spec/views/projects/edit.html.haml_spec.rb
Normal file
@ -0,0 +1,28 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "projects/edit", type: :view do
|
||||
let(:project) {
|
||||
Project.create!(
|
||||
title: "MyString",
|
||||
description: "MyText",
|
||||
user: nil
|
||||
)
|
||||
}
|
||||
|
||||
before(:each) do
|
||||
assign(:project, project)
|
||||
end
|
||||
|
||||
it "renders the edit project form" do
|
||||
render
|
||||
|
||||
assert_select "form[action=?][method=?]", project_path(project), "post" do
|
||||
|
||||
assert_select "input[name=?]", "project[title]"
|
||||
|
||||
assert_select "textarea[name=?]", "project[description]"
|
||||
|
||||
assert_select "input[name=?]", "project[user_id]"
|
||||
end
|
||||
end
|
||||
end
|
||||
26
spec/views/projects/index.html.haml_spec.rb
Normal file
26
spec/views/projects/index.html.haml_spec.rb
Normal file
@ -0,0 +1,26 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "projects/index", type: :view do
|
||||
before(:each) do
|
||||
assign(:projects, [
|
||||
Project.create!(
|
||||
title: "Title",
|
||||
description: "MyText",
|
||||
user: nil
|
||||
),
|
||||
Project.create!(
|
||||
title: "Title",
|
||||
description: "MyText",
|
||||
user: nil
|
||||
)
|
||||
])
|
||||
end
|
||||
|
||||
it "renders a list of projects" do
|
||||
render
|
||||
cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td'
|
||||
assert_select cell_selector, text: Regexp.new("Title".to_s), count: 2
|
||||
assert_select cell_selector, text: Regexp.new("MyText".to_s), count: 2
|
||||
assert_select cell_selector, text: Regexp.new(nil.to_s), count: 2
|
||||
end
|
||||
end
|
||||
24
spec/views/projects/new.html.haml_spec.rb
Normal file
24
spec/views/projects/new.html.haml_spec.rb
Normal file
@ -0,0 +1,24 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "projects/new", type: :view do
|
||||
before(:each) do
|
||||
assign(:project, Project.new(
|
||||
title: "MyString",
|
||||
description: "MyText",
|
||||
user: nil
|
||||
))
|
||||
end
|
||||
|
||||
it "renders new project form" do
|
||||
render
|
||||
|
||||
assert_select "form[action=?][method=?]", projects_path, "post" do
|
||||
|
||||
assert_select "input[name=?]", "project[title]"
|
||||
|
||||
assert_select "textarea[name=?]", "project[description]"
|
||||
|
||||
assert_select "input[name=?]", "project[user_id]"
|
||||
end
|
||||
end
|
||||
end
|
||||
18
spec/views/projects/show.html.haml_spec.rb
Normal file
18
spec/views/projects/show.html.haml_spec.rb
Normal file
@ -0,0 +1,18 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "projects/show", type: :view do
|
||||
before(:each) do
|
||||
assign(:project, Project.create!(
|
||||
title: "Title",
|
||||
description: "MyText",
|
||||
user: nil
|
||||
))
|
||||
end
|
||||
|
||||
it "renders attributes in <p>" do
|
||||
render
|
||||
expect(rendered).to match(/Title/)
|
||||
expect(rendered).to match(/MyText/)
|
||||
expect(rendered).to match(//)
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user