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 @@ + + +
+ +<%= notice %>
<%= alert %>
+ + <% if user_signed_in? %>