diff --git a/Gemfile b/Gemfile index fcd1f10..2adaed8 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ group :test do gem "minitest-rails" gem "minitest-spec-rails" gem "mocha" + gem "rails-controller-testing" gem "selenium-webdriver" gem "shoulda-context" gem "shoulda-matchers" diff --git a/Gemfile.lock b/Gemfile.lock index e19305b..75b53e9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -275,6 +275,10 @@ GEM rackup (2.1.0) rack (>= 3) webrick (~> 1.8) + rails-controller-testing (1.0.5) + actionpack (>= 5.0.1.rc1) + actionview (>= 5.0.1.rc1) + activesupport (>= 5.0.1.rc1) rails-dom-testing (2.2.0) activesupport (>= 5.0.0) minitest @@ -419,6 +423,7 @@ DEPENDENCIES pry-rails puma (>= 5.0) rails! + rails-controller-testing rbui! rubocop-rails-omakase selenium-webdriver diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3b5efbc..41c3366 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ class ApplicationController < ActionController::Base include Authentication + include Organizationable # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. # TODO: Turn on after styling is complete. # allow_browser versions: :modern diff --git a/app/controllers/concerns/organizationable.rb b/app/controllers/concerns/organizationable.rb new file mode 100644 index 0000000..593ea27 --- /dev/null +++ b/app/controllers/concerns/organizationable.rb @@ -0,0 +1,21 @@ +module Organizationable + extend ActiveSupport::Concern + + included do + before_action :assign_organization + end + + def current_organization + Current.organization || Current.user.base_organization + end + + def assign_organization + return unless Current.user + + Current.organization = find_organization_by_cookie + end + + def find_organization_by_cookie + Organization.find_by(id: cookies.signed[:organization_id]) || Current.user.base_organization + end +end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index ddc7416..7282aa3 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -4,7 +4,7 @@ class PostsController < ApplicationController # GET /posts def index - @posts = Post.all + @posts = current_organization.posts.order(created_at: :desc) end def show @@ -47,7 +47,12 @@ def set_post end def post_params - params.expect(post: [ :title, :message, :user_id ]).merge(user_id: current_user.id) + params.expect( + post: [ :title, :message, :user_id ] + ).merge( + user_id: current_user.id, + organization_id: current_organization.id + ) end def authorize_user! diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 8af29e1..b24fbb5 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -10,7 +10,7 @@ def create User.transaction do @user = User.new(user_params) if @user.save - create_organization_for(@user) + @user.create_base_organization! start_new_session_for(@user) redirect_to root_path, notice: "Welcome to PostIt! Start adding projects and tasks." else @@ -32,11 +32,4 @@ def user_params ] ) end - - def create_organization_for(user) - Organization.transaction do - organization = Organization.create!(name: user.name) - user.memberships.create!(organization: organization, role: :owner) - end - end end diff --git a/app/models/current.rb b/app/models/current.rb index 2bef56d..18ecc59 100644 --- a/app/models/current.rb +++ b/app/models/current.rb @@ -1,4 +1,4 @@ class Current < ActiveSupport::CurrentAttributes - attribute :session + attribute :session, :organization delegate :user, to: :session, allow_nil: true end diff --git a/app/models/organization.rb b/app/models/organization.rb index 0cf65d1..5ec8fbc 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -11,4 +11,5 @@ class Organization < ApplicationRecord has_many :memberships, dependent: :destroy has_many :users, through: :memberships + has_many :posts, dependent: :destroy end diff --git a/app/models/post.rb b/app/models/post.rb index 4b3789b..d0788c9 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -3,6 +3,7 @@ class Post < ApplicationRecord # Associations #----------------------------------------------------------------------------- belongs_to :user + belongs_to :organization has_rich_text :message #----------------------------------------------------------------------------- diff --git a/app/models/user.rb b/app/models/user.rb index c95f7ec..0ba025e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,4 +27,18 @@ class User < ApplicationRecord #----------------------------------------------------------------------------- normalizes :email_address, with: ->(e) { e.strip.downcase } + + #----------------------------------------------------------------------------- + # Instance Methods + #----------------------------------------------------------------------------- + + def base_organization + organizations.joins(:memberships).merge(Membership.owner).first || create_base_organization! + end + + def create_base_organization! + organization = Organization.create!(name: name) + memberships.create!(organization: organization, role: :owner) + organization + end end diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 9aa99cb..aa4923b 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -1,28 +1,17 @@
<%= notice %>
- <% end %> - <%= render @post %> <%= render RBUI::Link.new(href: edit_post_path(@post), variant: :outline, class: "me-3") { "Edit" } %> - <%#= render RBUI::Link.new( - href: post_path(@post), - variant: :destructive, - data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to delete this post?" }, - class: "ms-3") { - "Delete" - } %> - <%= render Components::ConfirmDialog.new( - variant: :destructive, - href: post_path(@post), - title: "Are you sure?", - description: "This action cannot be undone. This will permanently delete your post.", - action_text: "Delete post", - cancel_text: "Cancel", - data: { turbo_method: :delete }, - ) { "Delete" } %> + <%= render Components::ConfirmDialog.new( + variant: :destructive, + href: post_path(@post), + title: "Are you sure?", + description: "This action cannot be undone. This will permanently delete your post.", + action_text: "Delete post", + cancel_text: "Cancel", + data: { turbo_method: :delete }, + ) { "Delete" } %>