diff --git a/Gemfile b/Gemfile index 6fe5fac..dbaac1c 100644 --- a/Gemfile +++ b/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' diff --git a/Gemfile.lock b/Gemfile.lock index 07dc674..6c528f9 100644 --- a/Gemfile.lock +++ b/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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1c07694..71e2044 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb new file mode 100644 index 0000000..05c23b0 --- /dev/null +++ b/app/controllers/conversations_controller.rb @@ -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 diff --git a/app/controllers/listings_controller.rb b/app/controllers/listings_controller.rb index 20d407e..b0f989e 100644 --- a/app/controllers/listings_controller.rb +++ b/app/controllers/listings_controller.rb @@ -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 diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb new file mode 100644 index 0000000..f5ae541 --- /dev/null +++ b/app/controllers/messages_controller.rb @@ -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 diff --git a/app/models/conversation.rb b/app/models/conversation.rb new file mode 100644 index 0000000..5d7a6ba --- /dev/null +++ b/app/models/conversation.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb new file mode 100644 index 0000000..8ed3c92 --- /dev/null +++ b/app/models/message.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 52f3dfb..44a01de 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/conversations/index.html.erb b/app/views/conversations/index.html.erb new file mode 100644 index 0000000..4d23f61 --- /dev/null +++ b/app/views/conversations/index.html.erb @@ -0,0 +1,43 @@ + + + + + Conversations + + + <%= link_to "Back to Listings", listings_path %> +
+

Mailbox

+ +
+
+ <% @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 %> +
+
+
+ + +
+

All Users

+
+ <% @users.each do |user| %> + <% if user.id != current_user.id %> +
+ <%= user.email %> <%= link_to 'Message me!', conversations_path(sender_id: current_user.id, recipient_id: user.id), method: 'post'%> +
+ <% end %> + <% end %> +
+
+ + + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 99d812d..81dc940 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -3,15 +3,19 @@ Rently <%= csrf_meta_tags %> - + + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> +

<%= notice %>

<%= alert %>

+ + <% if user_signed_in? %>
  • @@ -23,7 +27,6 @@
  • <% end %> - <% if user_signed_in? %>
  • @@ -35,6 +38,8 @@
  • <% end %> + <%= link_to "Mailbox", conversations_path %>
    + <%= yield %> diff --git a/app/views/listings/index.html.erb b/app/views/listings/index.html.erb index 513ed74..b2a2158 100644 --- a/app/views/listings/index.html.erb +++ b/app/views/listings/index.html.erb @@ -1,4 +1,4 @@ -

    <%= notice %>

    +

    Listings

    @@ -22,9 +22,23 @@ - + <% @listings.each do |listing| %> - +
    +
    +
    +
    + <%= link_to cl_image_tag(listing.photo_url, :class => "uploaded_photo"), listing %> + Card Title + fav +
    +
    +

    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.

    +
    +
    +
    +
    + <% end %> - + + + +
    diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb new file mode 100644 index 0000000..f6de7c6 --- /dev/null +++ b/app/views/messages/index.html.erb @@ -0,0 +1,43 @@ + + + + + Messages + + + <% if @over_ten %> + <%= link_to 'Show Previous', '?m=all' %> + <% end %> + +
    + <% @messages.each do |message| %> + <% if message.body %> + <% user = User.find(message.user_id) %> +
    +
    +
    <%= user.email %> <%= time_ago_in_words(message.created_at)%> ago
    +
    +
    + + <%= message.body %> +
    +
    +
    +
    + <% end %> + <% end %> +
    + + <%= form_for [@conversation, @message], html: {class: "ui reply form"} do |f| %> +
    + <%= f.text_area :body, class: "form-control" %> +
    + <%= f.text_field :user_id, value: current_user.id, type: "hidden" %> +
    + <%= f.submit "Add Reply", class: "ui blue labeled submit icon button" %> +
    + + <% end %> + + + diff --git a/config/routes.rb b/config/routes.rb index ba0e842..2eb995e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20171108052249_create_conversations.rb b/db/migrate/20171108052249_create_conversations.rb new file mode 100644 index 0000000..53c98a5 --- /dev/null +++ b/db/migrate/20171108052249_create_conversations.rb @@ -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 diff --git a/db/migrate/20171108052557_create_messages.rb b/db/migrate/20171108052557_create_messages.rb new file mode 100644 index 0000000..886dffd --- /dev/null +++ b/db/migrate/20171108052557_create_messages.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index e19c432..3e9da5d 100644 --- a/db/schema.rb +++ b/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