mirror of
https://github.com/kingomarnajjar/curly-happiness.git
synced 2026-07-25 22:27:18 +10:00
added internal messaging, fixed up user roles
This commit is contained in:
parent
fe7e1a1f4f
commit
50e6c1124e
17 changed files with 256 additions and 21 deletions
5
Gemfile
5
Gemfile
|
|
@ -4,12 +4,15 @@ git_source(:github) do |repo_name|
|
|||
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
|
||||
"https://github.com/#{repo_name}.git"
|
||||
end
|
||||
# for styling
|
||||
# gem 'semantic-ui-sass', github: 'doabit/semantic-ui-sass'
|
||||
|
||||
#File uploads rails gem
|
||||
gem 'carrierwave', '~> 1.0', github: 'carrierwaveuploader/carrierwave'
|
||||
gem 'carrierwave'
|
||||
#Cloud storage for file uploads, created an account on cloudinary
|
||||
gem 'cloudinary'
|
||||
|
||||
#For roles, admin and user
|
||||
gem 'petergate'
|
||||
#For different Roles management
|
||||
# gem 'rolify'
|
||||
|
|
|
|||
15
Gemfile.lock
15
Gemfile.lock
|
|
@ -1,12 +1,3 @@
|
|||
GIT
|
||||
remote: https://github.com/carrierwaveuploader/carrierwave.git
|
||||
revision: e9f3be59e6e6b5d41a9b379df92cad6be16e7f84
|
||||
specs:
|
||||
carrierwave (1.2.1)
|
||||
activemodel (>= 4.0.0)
|
||||
activesupport (>= 4.0.0)
|
||||
mime-types (>= 1.16)
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
|
|
@ -62,6 +53,10 @@ GEM
|
|||
rack (>= 1.0.0)
|
||||
rack-test (>= 0.5.4)
|
||||
xpath (~> 2.0)
|
||||
carrierwave (1.2.1)
|
||||
activemodel (>= 4.0.0)
|
||||
activesupport (>= 4.0.0)
|
||||
mime-types (>= 1.16)
|
||||
childprocess (0.8.0)
|
||||
ffi (~> 1.0, >= 1.0.11)
|
||||
cloudinary (1.8.1)
|
||||
|
|
@ -238,7 +233,7 @@ PLATFORMS
|
|||
DEPENDENCIES
|
||||
byebug
|
||||
capybara (~> 2.13)
|
||||
carrierwave (~> 1.0)!
|
||||
carrierwave
|
||||
cloudinary
|
||||
coffee-rails (~> 4.2)
|
||||
devise
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
class ApplicationController < ActionController::Base
|
||||
protect_from_forgery with: :exception
|
||||
|
||||
|
||||
# # one other option that might seem a bit weird is to put a group of roles in an array:
|
||||
# access [:all, :user] => [:show, :index]
|
||||
end
|
||||
|
|
|
|||
21
app/controllers/conversations_controller.rb
Normal file
21
app/controllers/conversations_controller.rb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class ConversationsController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
def index
|
||||
@users = User.all
|
||||
@conversations = Conversation.all
|
||||
end
|
||||
def create
|
||||
if Conversation.between(params[:sender_id],params[:recipient_id])
|
||||
.present?
|
||||
@conversation = Conversation.between(params[:sender_id],
|
||||
params[:recipient_id]).first
|
||||
else
|
||||
@conversation = Conversation.create!(conversation_params)
|
||||
end
|
||||
redirect_to conversation_messages_path(@conversation)
|
||||
end
|
||||
private
|
||||
def conversation_params
|
||||
params.permit(:sender_id, :recipient_id)
|
||||
end
|
||||
end
|
||||
|
|
@ -1,5 +1,9 @@
|
|||
class ListingsController < ApplicationController
|
||||
before_action :set_listing, only: [:show, :edit, :update, :destroy]
|
||||
#Configure for petergates roles "access all"
|
||||
access all: [:show, :index, :new, :edit, :create, :update ], user: {except: [:destroy, ]}, message: "You shall not pass", admin: :all
|
||||
|
||||
|
||||
# params.require(:listing).permit({photo: []})
|
||||
# GET /listings
|
||||
# GET /listings.json
|
||||
|
|
|
|||
35
app/controllers/messages_controller.rb
Normal file
35
app/controllers/messages_controller.rb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
class MessagesController < ApplicationController
|
||||
before_action do
|
||||
@conversation = Conversation.find(params[:conversation_id])
|
||||
end
|
||||
def index
|
||||
@messages = @conversation.messages
|
||||
if @messages.length > 10
|
||||
@over_ten = true
|
||||
@messages = @messages[-10..-1]
|
||||
end
|
||||
if params[:m]
|
||||
@over_ten = false
|
||||
@messages = @conversation.messages
|
||||
end
|
||||
if @messages.last
|
||||
if @messages.last.user_id != current_user.id
|
||||
@messages.last.read = true;
|
||||
end
|
||||
end
|
||||
@message = @conversation.messages.new
|
||||
end
|
||||
def new
|
||||
@message = @conversation.messages.new
|
||||
end
|
||||
def create
|
||||
@message = @conversation.messages.new(message_params)
|
||||
if @message.save
|
||||
redirect_to conversation_messages_path(@conversation)
|
||||
end
|
||||
end
|
||||
private
|
||||
def message_params
|
||||
params.require(:message).permit(:body, :user_id)
|
||||
end
|
||||
end
|
||||
12
app/models/conversation.rb
Normal file
12
app/models/conversation.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
class Conversation < ActiveRecord::Base
|
||||
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
|
||||
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
|
||||
has_many :messages, dependent: :destroy
|
||||
|
||||
validates_uniqueness_of :sender_id, :scope => :recipient_id
|
||||
|
||||
scope :between, -> (sender_id,recipient_id) do
|
||||
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
|
||||
end
|
||||
|
||||
end
|
||||
10
app/models/message.rb
Normal file
10
app/models/message.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
class Message < ActiveRecord::Base
|
||||
belongs_to :conversation
|
||||
belongs_to :user
|
||||
validates_presence_of :body, :conversation_id, :user_id
|
||||
|
||||
# def message_time
|
||||
# created_at.strftime("%m/%d/%y at %l:%M %p")
|
||||
# end
|
||||
|
||||
end
|
||||
|
|
@ -4,9 +4,10 @@ class User < ApplicationRecord
|
|||
## The :user role is added by default and shouldn't be included in this list. ##
|
||||
## The :root_admin can access any page regardless of access settings. Use with caution! ##
|
||||
## The multiple option can be set to true if you need users to have multiple roles. ##
|
||||
petergate(roles: [:admin, :editor], multiple: false) ##
|
||||
petergate(roles: [:admin, ], multiple: false) ##
|
||||
############################################################################################
|
||||
|
||||
|
||||
has_many :conversations, :foreign_key => :sender_id
|
||||
|
||||
# Include default devise modules. Others available are:
|
||||
# :confirmable, :lockable, :timeoutable and :omniauthable
|
||||
|
|
|
|||
43
app/views/conversations/index.html.erb
Normal file
43
app/views/conversations/index.html.erb
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Conversations</title>
|
||||
</head>
|
||||
<body>
|
||||
<%= link_to "Back to Listings", listings_path %>
|
||||
<div class="ui segment">
|
||||
<h3>Mailbox</h3>
|
||||
|
||||
<div class="ui list">
|
||||
<div class="item">
|
||||
<% @conversations.each do |conversation| %>
|
||||
<% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %>
|
||||
<% if conversation.sender_id == current_user.id %>
|
||||
<% recipient = User.find(conversation.recipient_id) %>
|
||||
<% else %>
|
||||
<% recipient = User.find(conversation.sender_id) %>
|
||||
<% end %>
|
||||
<%= link_to recipient.email, conversation_messages_path(conversation)%>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="ui segment">
|
||||
<h3>All Users</h3>
|
||||
<div class="ui list">
|
||||
<% @users.each do |user| %>
|
||||
<% if user.id != current_user.id %>
|
||||
<div class="item">
|
||||
<%= user.email %> <%= link_to 'Message me!', conversations_path(sender_id: current_user.id, recipient_id: user.id), method: 'post'%>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -3,15 +3,19 @@
|
|||
<head>
|
||||
<title>Rently</title>
|
||||
<%= csrf_meta_tags %>
|
||||
|
||||
<!-- Full version, For production do compiled and minified CSS by adding .min before .css -->
|
||||
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.css"> -->
|
||||
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
||||
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<!--For devise to flash notices and alerts -->
|
||||
<p class="notice"><%= notice %></p>
|
||||
<p class="alert"><%= alert %></p>
|
||||
|
||||
|
||||
<!-- # views/devise/menu/_login_items.html.erb -->
|
||||
<% if user_signed_in? %>
|
||||
<li>
|
||||
|
|
@ -23,7 +27,6 @@
|
|||
</li>
|
||||
<% end %>
|
||||
|
||||
|
||||
<!-- # views/devise/menu/_registration_items.html.erb -->
|
||||
<% if user_signed_in? %>
|
||||
<li>
|
||||
|
|
@ -35,6 +38,8 @@
|
|||
</li>
|
||||
<% end %>
|
||||
|
||||
<%= link_to "Mailbox", conversations_path %><br>
|
||||
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<p id="notice"><%= notice %></p>
|
||||
<!-- <p id="notice"><%= notice %></p> -->
|
||||
|
||||
<h1>Listings</h1>
|
||||
|
||||
|
|
@ -22,9 +22,23 @@
|
|||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<body>
|
||||
<% @listings.each do |listing| %>
|
||||
<tr>
|
||||
<div class="row">
|
||||
<div class="col s12 m6">
|
||||
<div class="card">
|
||||
<div class="card-image">
|
||||
<%= link_to cl_image_tag(listing.photo_url, :class => "uploaded_photo"), listing %>
|
||||
<span class="card-title">Card Title</span>
|
||||
<a class="btn-floating halfway-fab waves-effect waves-light red"><i class="material-icons">fav</i></a>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
<p>I am a very simple card. I am good at containing small bits of information. I am convenient because I require little markup to use effectively.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- <tr>
|
||||
<td><%= cl_image_tag(listing.photo_url, :class => "uploaded_photo") %></td>
|
||||
<td><%= cl_image_tag(listing.photo2_url, :class => "uploaded_photo") %></td>
|
||||
<td><%= cl_image_tag(listing.photo3_url, :class => "uploaded_photo") %></td>
|
||||
|
|
@ -41,9 +55,12 @@
|
|||
<td><%= link_to 'Show', listing %></td>
|
||||
<td><%= link_to 'Edit', edit_listing_path(listing) %></td>
|
||||
<td><%= link_to 'Destroy', listing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
</tr> -->
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
</table>
|
||||
|
||||
<br>
|
||||
|
|
|
|||
43
app/views/messages/index.html.erb
Normal file
43
app/views/messages/index.html.erb
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Messages</title>
|
||||
</head>
|
||||
<body>
|
||||
<% if @over_ten %>
|
||||
<%= link_to 'Show Previous', '?m=all' %>
|
||||
<% end %>
|
||||
|
||||
<div class="ui segment">
|
||||
<% @messages.each do |message| %>
|
||||
<% if message.body %>
|
||||
<% user = User.find(message.user_id) %>
|
||||
<div class="item">
|
||||
<div class="content">
|
||||
<div class="header"><strong><%= user.email %></strong> <%= time_ago_in_words(message.created_at)%> ago</div>
|
||||
<div class="list">
|
||||
<div class="item">
|
||||
<i class="right triangle icon"></i>
|
||||
<%= message.body %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= form_for [@conversation, @message], html: {class: "ui reply form"} do |f| %>
|
||||
<div class="field">
|
||||
<%= f.text_area :body, class: "form-control" %>
|
||||
</div>
|
||||
<%= f.text_field :user_id, value: current_user.id, type: "hidden" %>
|
||||
<div>
|
||||
<%= f.submit "Add Reply", class: "ui blue labeled submit icon button" %>
|
||||
</div>
|
||||
|
||||
<% end %>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -4,6 +4,9 @@ Rails.application.routes.draw do
|
|||
devise_for :users
|
||||
get 'home/index'
|
||||
|
||||
resources :conversations do
|
||||
resources :messages
|
||||
end
|
||||
|
||||
root to: "home#index"
|
||||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
||||
|
|
|
|||
9
db/migrate/20171108052249_create_conversations.rb
Normal file
9
db/migrate/20171108052249_create_conversations.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
class CreateConversations < ActiveRecord::Migration[5.1]
|
||||
def change
|
||||
create_table :conversations do |t|
|
||||
t.integer :sender_id
|
||||
t.integer :recipient_id
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
12
db/migrate/20171108052557_create_messages.rb
Normal file
12
db/migrate/20171108052557_create_messages.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
class CreateMessages < ActiveRecord::Migration[5.1]
|
||||
def change
|
||||
create_table :messages do |t|
|
||||
t.text :body
|
||||
t.references :conversation, index: true
|
||||
t.references :user, index: true
|
||||
t.boolean :read, :default => false
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
20
db/schema.rb
20
db/schema.rb
|
|
@ -10,11 +10,18 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20171107062009) do
|
||||
ActiveRecord::Schema.define(version: 20171108052557) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
create_table "conversations", force: :cascade do |t|
|
||||
t.integer "sender_id"
|
||||
t.integer "recipient_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "listings", force: :cascade do |t|
|
||||
t.text "photo"
|
||||
t.text "photo2"
|
||||
|
|
@ -29,6 +36,17 @@ ActiveRecord::Schema.define(version: 20171107062009) do
|
|||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "messages", force: :cascade do |t|
|
||||
t.text "body"
|
||||
t.bigint "conversation_id"
|
||||
t.bigint "user_id"
|
||||
t.boolean "read", default: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["conversation_id"], name: "index_messages_on_conversation_id"
|
||||
t.index ["user_id"], name: "index_messages_on_user_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue