Add user profiles

This commit is contained in:
Jesse C. Fisher 2023-11-06 17:17:00 +00:00
parent ef0c9e69a0
commit 18a12afb78
32 changed files with 563 additions and 14 deletions

View 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/

View File

@ -207,4 +207,9 @@ textarea
background: #00ff0060
border-radius: 0 0 0 79em
.contain
width: 100%
max-width: 1200px
padding: 1em
@import doodads

View File

@ -0,0 +1,3 @@
// Place all the styles related to the Profiles controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/

View File

@ -8,7 +8,7 @@ class ApplicationController < ActionController::Base
before_action :get_footer_tokens
if (Rails.env.development? || Rails.env.test?)
include Pundit
include Pundit::Authorization
after_action :verify_authorized, unless: -> { devise_controller? }
# after_action :verify_policy_scoped, only: :index
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

View File

@ -0,0 +1,74 @@
class ProfilesController < ApplicationController
before_action :set_profile, only: %i[ show edit update destroy ]
# GET /profiles or /profiles.json
def index
@profiles = policy_scope Profile.all
authorize @profiles
end
# GET /profiles/1 or /profiles/1.json
def show
authorize @profile
end
# GET /profiles/new
def new
authorize @profile = current_user.profiles.new
end
# GET /profiles/1/edit
def edit
authorize @profile
end
# POST /profiles or /profiles.json
def create
authorize @profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to profile_url(@profile), notice: "Profile was successfully created." }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1 or /profiles/1.json
def update
authorize @profile
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to profile_url(@profile), notice: "Profile was successfully updated." }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1 or /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: "Profile was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Only allow a list of trusted parameters through.
def profile_params
params.require(:profile).permit(:user_id, :name, :summary, :about)
end
end

View File

@ -0,0 +1,2 @@
module ProfilesHelper
end

3
app/models/profile.rb Normal file
View File

@ -0,0 +1,3 @@
class Profile < ActiveRecord::Base
belongs_to :user
end

View File

@ -6,6 +6,7 @@ class User < ActiveRecord::Base
has_many :canisters
has_many :doodads
has_many :projects
has_many :profiles
def icid
stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize

View File

@ -0,0 +1,55 @@
class ProfilePolicy
attr_reader :user, :model
def initialize(user, model)
@user = user || User.new
@profile = model
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?
@user.admin? || true
end
def show?
@user.admin? || true
end
def new?
@user.signed_in?
end
def create?
@user.signed_in?
end
def edit?
update?
end
def update?
@user.admin? || @user == @profile.user
end
def destroy?
@user.admin? || @user == @profile.user
end
end

View File

@ -2,7 +2,7 @@
<ul>
<li><%= link_to 'Public', root_path, data: { turbo: false } %></li>
<li><%= link_to 'Tokens', tokens_path %></li>
<li><%= link_to 'People', users_path %></li>
<li><%= link_to 'People', profiles_path %></li>
<li><%= link_to 'Projects', projects_path %></li>
<li><%= link_to 'Doodads', doodads_path, data: { turbo: false } %></li>
<% if !request.base_url.match(/pin|my|fireside/) %>

View File

@ -0,0 +1,12 @@
= simple_form_for(@profile) do |f|
= f.error_notification
.form-inputs
= f.hidden_field :user_id
= f.input :name
= f.label :summary
= f.input :summary, as: :string, label: false, input_html: { style: "width: 100%;"}
= f.input :about
.form-actions
= f.button :submit

View File

@ -0,0 +1,2 @@
json.extract! profile, :id, :user_id, :name, :summary, :about, :created_at, :updated_at
json.url profile_url(profile, format: :json)

View File

@ -0,0 +1,3 @@
%h1 Editing profile
= render 'form'

View File

@ -0,0 +1,31 @@
%h1 Listing profiles
%p
- if policy(Profile).new?
= link_to '+ New Profile', new_profile_path
- else
= link_to 'Sign In', new_user_session_path
to create a new Profile
%table
%thead
%tr
-# %th User
%th Name
%th Summary
%th About
-# - if policy(Profile).destroy?
-# %th
-# %th
%tbody
- @profiles.each do |profile|
%tr
-# %td= profile.user
%td= link_to profile.name, profile
%td= profile.summary
%td= profile.about
-# - if policy(profile).destroy?
-# %td= link_to 'Edit', edit_profile_path(profile)
-# %td= link_to 'Destroy', profile, method: :delete, data: { confirm: 'Are you sure?' }

View File

@ -0,0 +1 @@
json.array! @profiles, partial: "profiles/profile", as: :profile

View File

@ -0,0 +1,5 @@
%h1 New profile
= render 'form'
= link_to 'Back', profiles_path

View File

@ -0,0 +1,14 @@
- if policy(@profile).edit?
.contain
= link_to 'Edit', edit_profile_path(@profile)
%p
= @profile.name
%p
= @profile.summary
%h2 About #{@profile.name}
.about
= @profile.about

View File

@ -0,0 +1 @@
json.partial! "profiles/profile", profile: @profile

View File

@ -1,11 +1,11 @@
module ActiveRecord
module Tasks
class PostgreSQLDatabaseTasks
def drop
establish_master_connection
connection.select_all "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='#{configuration['database']}' AND state='idle';"
connection.drop_database configuration['database']
end
end
end
end
# module ActiveRecord
# module Tasks
# class PostgreSQLDatabaseTasks
# def drop
# establish_master_connection
# connection.select_all "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='#{configuration['database']}' AND state='idle';"
# connection.drop_database configuration['database']
# end
# end
# end
# end

View File

@ -1,6 +1,8 @@
en:
pundit:
default: 'You cannot perform this action.'
profile_policy:
new?: 'You must sign in to create a new Profile!'
project_policy:
new?: 'You must sign in to create a new Project!'
post_policy:

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :profiles
resources :tokens
resources :projects
resources :doodads

View File

@ -0,0 +1,12 @@
class CreateProfiles < ActiveRecord::Migration[6.1]
def change
create_table :profiles do |t|
t.belongs_to :user, null: false, foreign_key: true
t.string :name
t.string :summary
t.text :about
t.timestamps
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2023_10_27_042335) do
ActiveRecord::Schema.define(version: 2023_11_06_161052) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -96,6 +96,16 @@ ActiveRecord::Schema.define(version: 2023_10_27_042335) do
t.index ["player_id"], name: "index_plays_on_player_id"
end
create_table "profiles", force: :cascade do |t|
t.bigint "user_id", null: false
t.string "name"
t.string "summary"
t.text "about"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_profiles_on_user_id"
end
create_table "projects", force: :cascade do |t|
t.string "title"
t.text "description"
@ -185,5 +195,6 @@ ActiveRecord::Schema.define(version: 2023_10_27_042335) do
add_foreign_key "canisters", "users"
add_foreign_key "doodads", "users"
add_foreign_key "profiles", "users"
add_foreign_key "projects", "users"
end

View File

@ -0,0 +1,8 @@
FactoryGirl.define do
factory :profile do
user nil
name "MyString"
summary "MyString"
about "MyText"
end
end

View File

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the ProfilesHelper. For example:
#
# describe ProfilesHelper 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 ProfilesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Profile, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View 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 "/profiles", type: :request do
# This should return the minimal set of attributes required to create a valid
# Profile. As you add validations to Profile, 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
Profile.create! valid_attributes
get profiles_url
expect(response).to be_successful
end
end
describe "GET /show" do
it "renders a successful response" do
profile = Profile.create! valid_attributes
get profile_url(profile)
expect(response).to be_successful
end
end
describe "GET /new" do
it "renders a successful response" do
get new_profile_url
expect(response).to be_successful
end
end
describe "GET /edit" do
it "renders a successful response" do
profile = Profile.create! valid_attributes
get edit_profile_url(profile)
expect(response).to be_successful
end
end
describe "POST /create" do
context "with valid parameters" do
it "creates a new Profile" do
expect {
post profiles_url, params: { profile: valid_attributes }
}.to change(Profile, :count).by(1)
end
it "redirects to the created profile" do
post profiles_url, params: { profile: valid_attributes }
expect(response).to redirect_to(profile_url(Profile.last))
end
end
context "with invalid parameters" do
it "does not create a new Profile" do
expect {
post profiles_url, params: { profile: invalid_attributes }
}.to change(Profile, :count).by(0)
end
it "renders a successful response (i.e. to display the 'new' template)" do
post profiles_url, params: { profile: 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 profile" do
profile = Profile.create! valid_attributes
patch profile_url(profile), params: { profile: new_attributes }
profile.reload
skip("Add assertions for updated state")
end
it "redirects to the profile" do
profile = Profile.create! valid_attributes
patch profile_url(profile), params: { profile: new_attributes }
profile.reload
expect(response).to redirect_to(profile_url(profile))
end
end
context "with invalid parameters" do
it "renders a successful response (i.e. to display the 'edit' template)" do
profile = Profile.create! valid_attributes
patch profile_url(profile), params: { profile: invalid_attributes }
expect(response).to be_successful
end
end
end
describe "DELETE /destroy" do
it "destroys the requested profile" do
profile = Profile.create! valid_attributes
expect {
delete profile_url(profile)
}.to change(Profile, :count).by(-1)
end
it "redirects to the profiles list" do
profile = Profile.create! valid_attributes
delete profile_url(profile)
expect(response).to redirect_to(profiles_url)
end
end
end

View File

@ -0,0 +1,38 @@
require "rails_helper"
RSpec.describe ProfilesController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "/profiles").to route_to("profiles#index")
end
it "routes to #new" do
expect(get: "/profiles/new").to route_to("profiles#new")
end
it "routes to #show" do
expect(get: "/profiles/1").to route_to("profiles#show", id: "1")
end
it "routes to #edit" do
expect(get: "/profiles/1/edit").to route_to("profiles#edit", id: "1")
end
it "routes to #create" do
expect(post: "/profiles").to route_to("profiles#create")
end
it "routes to #update via PUT" do
expect(put: "/profiles/1").to route_to("profiles#update", id: "1")
end
it "routes to #update via PATCH" do
expect(patch: "/profiles/1").to route_to("profiles#update", id: "1")
end
it "routes to #destroy" do
expect(delete: "/profiles/1").to route_to("profiles#destroy", id: "1")
end
end
end

View File

@ -0,0 +1,31 @@
require 'rails_helper'
RSpec.describe "profiles/edit", type: :view do
let(:profile) {
Profile.create!(
user: nil,
name: "MyString",
summary: "MyString",
about: "MyText"
)
}
before(:each) do
assign(:profile, profile)
end
it "renders the edit profile form" do
render
assert_select "form[action=?][method=?]", profile_path(profile), "post" do
assert_select "input[name=?]", "profile[user_id]"
assert_select "input[name=?]", "profile[name]"
assert_select "input[name=?]", "profile[summary]"
assert_select "textarea[name=?]", "profile[about]"
end
end
end

View File

@ -0,0 +1,29 @@
require 'rails_helper'
RSpec.describe "profiles/index", type: :view do
before(:each) do
assign(:profiles, [
Profile.create!(
user: nil,
name: "Name",
summary: "Summary",
about: "MyText"
),
Profile.create!(
user: nil,
name: "Name",
summary: "Summary",
about: "MyText"
)
])
end
it "renders a list of profiles" do
render
cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td'
assert_select cell_selector, text: Regexp.new(nil.to_s), count: 2
assert_select cell_selector, text: Regexp.new("Name".to_s), count: 2
assert_select cell_selector, text: Regexp.new("Summary".to_s), count: 2
assert_select cell_selector, text: Regexp.new("MyText".to_s), count: 2
end
end

View File

@ -0,0 +1,27 @@
require 'rails_helper'
RSpec.describe "profiles/new", type: :view do
before(:each) do
assign(:profile, Profile.new(
user: nil,
name: "MyString",
summary: "MyString",
about: "MyText"
))
end
it "renders new profile form" do
render
assert_select "form[action=?][method=?]", profiles_path, "post" do
assert_select "input[name=?]", "profile[user_id]"
assert_select "input[name=?]", "profile[name]"
assert_select "input[name=?]", "profile[summary]"
assert_select "textarea[name=?]", "profile[about]"
end
end
end

View File

@ -0,0 +1,20 @@
require 'rails_helper'
RSpec.describe "profiles/show", type: :view do
before(:each) do
assign(:profile, Profile.create!(
user: nil,
name: "Name",
summary: "Summary",
about: "MyText"
))
end
it "renders attributes in <p>" do
render
expect(rendered).to match(//)
expect(rendered).to match(/Name/)
expect(rendered).to match(/Summary/)
expect(rendered).to match(/MyText/)
end
end