Add microposts
This commit is contained in:
parent
c45487404f
commit
ff3de26b48
1
Gemfile
1
Gemfile
@ -82,3 +82,4 @@ gem 'inline_svg'
|
||||
gem 'importmap-rails'
|
||||
gem 'turbo-rails'
|
||||
gem 'stimulus-rails'
|
||||
gem 'will_paginate'
|
||||
|
||||
@ -481,6 +481,7 @@ GEM
|
||||
websocket-driver (0.7.6)
|
||||
websocket-extensions (>= 0.1.0)
|
||||
websocket-extensions (0.1.5)
|
||||
will_paginate (4.0.0)
|
||||
xpath (3.2.0)
|
||||
nokogiri (~> 1.8)
|
||||
zeitwerk (2.6.12)
|
||||
@ -559,6 +560,7 @@ DEPENDENCIES
|
||||
turbo-rails
|
||||
uglifier
|
||||
webdrivers
|
||||
will_paginate
|
||||
|
||||
RUBY VERSION
|
||||
ruby 2.7.7p221
|
||||
|
||||
3
app/assets/javascripts/microposts.coffee
Normal file
3
app/assets/javascripts/microposts.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/
|
||||
@ -176,6 +176,7 @@ footer
|
||||
background: #e1b02733
|
||||
box-shadow: 0px 0px 8px #00000042
|
||||
border-radius: 12em
|
||||
display: inline-block
|
||||
|
||||
#skyspaces
|
||||
width: 100%
|
||||
|
||||
3
app/assets/stylesheets/microposts.scss
Normal file
3
app/assets/stylesheets/microposts.scss
Normal file
@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the Microposts controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: https://sass-lang.com/
|
||||
21
app/controllers/microposts_controller.rb
Normal file
21
app/controllers/microposts_controller.rb
Normal file
@ -0,0 +1,21 @@
|
||||
class MicropostsController < ApplicationController
|
||||
def create
|
||||
@micropost = current_user.microposts.build(micropost_params)
|
||||
authorize @micropost
|
||||
if @micropost.save
|
||||
flash[:success] = "Micropost created!"
|
||||
redirect_to root_url
|
||||
else
|
||||
render 'users/show'
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def micropost_params
|
||||
params.require(:micropost).permit(:content)
|
||||
end
|
||||
end
|
||||
@ -8,10 +8,22 @@ class UsersController < ApplicationController
|
||||
end
|
||||
|
||||
def show
|
||||
@user = User.find(params[:id])
|
||||
@user ||= User.find(params[:id])
|
||||
@micropost = current_user.microposts.build if current_user.signed_in?
|
||||
@microposts = @user.microposts.paginate(page: params[:page])
|
||||
authorize @user
|
||||
end
|
||||
|
||||
def home
|
||||
authorize current_user
|
||||
# @user = current_user
|
||||
# @micropost = current_user.microposts.build if current_user.signed_in?
|
||||
# @microposts = @user.microposts.paginate(page: params[:page])
|
||||
# authorize @user
|
||||
redirect_to current_user
|
||||
end
|
||||
|
||||
|
||||
def update
|
||||
@user = User.find(params[:id])
|
||||
authorize @user
|
||||
|
||||
2
app/helpers/microposts_helper.rb
Normal file
2
app/helpers/microposts_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module MicropostsHelper
|
||||
end
|
||||
6
app/models/micropost.rb
Normal file
6
app/models/micropost.rb
Normal file
@ -0,0 +1,6 @@
|
||||
class Micropost < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
default_scope -> { order(created_at: :desc) }
|
||||
validates :user_id, presence: true
|
||||
validates :content, presence: true, length: { maximum: 140 }
|
||||
end
|
||||
@ -11,7 +11,7 @@ class User < ActiveRecord::Base
|
||||
foreign_key: "follower_id",
|
||||
dependent: :destroy
|
||||
has_many :following, through: :active_relationships, source: :followed
|
||||
|
||||
has_many :microposts, dependent: :destroy
|
||||
|
||||
def icid
|
||||
stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize
|
||||
|
||||
55
app/policies/micropost_policy.rb
Normal file
55
app/policies/micropost_policy.rb
Normal file
@ -0,0 +1,55 @@
|
||||
class MicropostPolicy
|
||||
attr_reader :user, :model
|
||||
|
||||
def initialize(user, model)
|
||||
@user = user || User.new
|
||||
@micropost = 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 == @micropost.user
|
||||
end
|
||||
|
||||
def destroy?
|
||||
@user.admin? || @user == @micropost.user
|
||||
end
|
||||
end
|
||||
@ -33,6 +33,10 @@ class UserPolicy
|
||||
@current_user.admin? || @current_user == @user
|
||||
end
|
||||
|
||||
def home?
|
||||
@current_user.admin? || @current_user == @user
|
||||
end
|
||||
|
||||
def update?
|
||||
@current_user.admin?
|
||||
end
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
-# = link_to (image_tag "telegram-logo.png", height: 32, width: 32), "https://t.me/+IEp-tAetjYozYWM5"
|
||||
-# - @tokens = Token.all
|
||||
.footer-tokens
|
||||
-# .footer-tokens
|
||||
%h3 Tokens
|
||||
- @footer_tokens.each do |token|
|
||||
= link_to token.name, token
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
<nav>
|
||||
<ul>
|
||||
<%# <% if current_user.signed_in? %1> %>
|
||||
<%# <li><%= link_to 'My Zone', user_path(current_user), data: { turbo: false } %1></li> %>
|
||||
<%# <% end %1> %>
|
||||
<li><%= link_to 'Public', root_path, data: { turbo: false } %></li>
|
||||
<% if current_user.signed_in? %>
|
||||
<li><%= link_to 'My Tendy Zone', root_path, data: { turbo: false } %></li>
|
||||
<% end %>
|
||||
<li><%= link_to 'Public', visitors_path, data: { turbo: false } %></li>
|
||||
<li><%= link_to 'Tokens', tokens_path %></li>
|
||||
<li><%= link_to 'Profiles', profiles_path %></li>
|
||||
<li><%= link_to 'Projects', projects_path %></li>
|
||||
|
||||
4
app/views/microposts/_micropost.html.haml
Normal file
4
app/views/microposts/_micropost.html.haml
Normal file
@ -0,0 +1,4 @@
|
||||
%li{id: "micropost-#{micropost.id}"}
|
||||
%span= link_to micropost.user.display_name, micropost.user
|
||||
%span.content= micropost.content
|
||||
%span.timestamp Posted #{time_ago_in_words(micropost.created_at)} ago.
|
||||
6
app/views/shared/_micropost_form.html.haml
Normal file
6
app/views/shared/_micropost_form.html.haml
Normal file
@ -0,0 +1,6 @@
|
||||
= simple_form_for(@micropost) do |f|
|
||||
= f.error_notification
|
||||
.form-inputs
|
||||
= f.text_area :content, placeholder: "I got something to say..."
|
||||
.form-actions
|
||||
= f.button :submit, "Post"
|
||||
@ -1,7 +0,0 @@
|
||||
<h3>User</h3>
|
||||
<p>Name: <%= @user.display_name if @user.display_name %></p>
|
||||
<%# <p>Email: <%= @user.email if @user.email %1></p> %>
|
||||
|
||||
<section class="stats">
|
||||
<%= render 'shared/stats' %>
|
||||
</section>
|
||||
14
app/views/users/show.html.haml
Normal file
14
app/views/users/show.html.haml
Normal file
@ -0,0 +1,14 @@
|
||||
%section.micropost_form
|
||||
= render 'shared/micropost_form'
|
||||
|
||||
.contain
|
||||
%h1 #{@user.display_name if @user.display_name}
|
||||
%section.stats
|
||||
= render 'shared/stats'
|
||||
|
||||
- if @user.microposts.any?
|
||||
%section.microposts
|
||||
%h3 Posts #{@user.microposts.count}
|
||||
%ol.microposts
|
||||
= render @microposts
|
||||
= will_paginate @microposts
|
||||
@ -64,7 +64,6 @@ Rails.application.routes.draw do
|
||||
constraints host: 'theneedfuldo.tendy.zone' do
|
||||
root to: 'projects#show', id: 2, as: :needful_host_root
|
||||
end
|
||||
root to: 'visitors#index'
|
||||
get '/play-now', to: 'tables#play_now', as: 'play_now'
|
||||
get '/tutorial', to: 'tables#tutorial', as: 'tutorial'
|
||||
devise_for :users, controllers: { registrations: "users/registrations" }
|
||||
@ -81,4 +80,18 @@ Rails.application.routes.draw do
|
||||
end
|
||||
end
|
||||
resources :relationships, only: [:create, :destroy]
|
||||
resources :visitors, only: [:index]
|
||||
resources :microposts, only: [:create, :destroy]
|
||||
|
||||
devise_scope :user do
|
||||
authenticated do
|
||||
root 'users#home', as: :authenticated_root
|
||||
end
|
||||
|
||||
unauthenticated do
|
||||
root 'devise/sessions#new', as: :unauthenticated_root
|
||||
end
|
||||
end
|
||||
root to: 'visitors#index'
|
||||
|
||||
end
|
||||
|
||||
11
db/migrate/20231114160910_create_microposts.rb
Normal file
11
db/migrate/20231114160910_create_microposts.rb
Normal file
@ -0,0 +1,11 @@
|
||||
class CreateMicroposts < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :microposts do |t|
|
||||
t.text :content
|
||||
t.references :user, null: false, foreign_key: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
add_index :microposts, [:user_id, :created_at]
|
||||
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_11_09_015600) do
|
||||
ActiveRecord::Schema.define(version: 2023_11_14_160910) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@ -56,6 +56,15 @@ ActiveRecord::Schema.define(version: 2023_11_09_015600) do
|
||||
t.index ["play_to_beat_id"], name: "index_games_on_play_to_beat_id"
|
||||
end
|
||||
|
||||
create_table "microposts", force: :cascade do |t|
|
||||
t.text "content"
|
||||
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", "created_at"], name: "index_microposts_on_user_id_and_created_at"
|
||||
t.index ["user_id"], name: "index_microposts_on_user_id"
|
||||
end
|
||||
|
||||
create_table "play_to_beats", force: :cascade do |t|
|
||||
t.integer "game_id"
|
||||
t.integer "play_id"
|
||||
@ -213,6 +222,7 @@ ActiveRecord::Schema.define(version: 2023_11_09_015600) do
|
||||
|
||||
add_foreign_key "canisters", "users"
|
||||
add_foreign_key "doodads", "users"
|
||||
add_foreign_key "microposts", "users"
|
||||
add_foreign_key "profiles", "users"
|
||||
add_foreign_key "projects", "users"
|
||||
add_foreign_key "social_links", "profiles"
|
||||
|
||||
@ -8,3 +8,10 @@
|
||||
user = CreateAdminService.new.call
|
||||
puts 'CREATED ADMIN USER: ' << user.email
|
||||
# Environment variables (ENV['...']) can be set in the file .env file.
|
||||
|
||||
# Microposts
|
||||
# users = User.order(:created_at).take(6)
|
||||
# 50.times do
|
||||
# content = Faker::Lorem.sentence(5)
|
||||
# users.each { |user| user.microposts.create!(content: content) }
|
||||
# end
|
||||
|
||||
6
spec/factories/microposts.rb
Normal file
6
spec/factories/microposts.rb
Normal file
@ -0,0 +1,6 @@
|
||||
FactoryGirl.define do
|
||||
factory :micropost do
|
||||
content "MyText"
|
||||
user nil
|
||||
end
|
||||
end
|
||||
15
spec/helpers/microposts_helper_spec.rb
Normal file
15
spec/helpers/microposts_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 MicropostsHelper. For example:
|
||||
#
|
||||
# describe MicropostsHelper 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 MicropostsHelper, type: :helper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
5
spec/models/micropost_spec.rb
Normal file
5
spec/models/micropost_spec.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Micropost, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
7
spec/requests/microposts_spec.rb
Normal file
7
spec/requests/microposts_spec.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "Microposts", type: :request do
|
||||
describe "GET /index" do
|
||||
pending "add some examples (or delete) #{__FILE__}"
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user