adds direct messages

This commit is contained in:
rgarcia
2016-06-08 20:44:54 +02:00
parent 6bade16ee8
commit c3d06c8bd0
13 changed files with 119 additions and 2 deletions

View File

@@ -0,0 +1,35 @@
class DirectMessagesController < ApplicationController
skip_authorization_check
def new
@receiver = User.find(params[:user_id])
@direct_message = DirectMessage.new(receiver: @receiver)
end
def create
@sender = current_user
@receiver = User.find(params[:user_id])
@direct_message = DirectMessage.new(parsed_params)
if @direct_message.save
Mailer.direct_message(@direct_message).deliver_later
redirect_to [@receiver, @direct_message], notice: I18n.t("flash.actions.create.direct_message")
else
render :new
end
end
def show
@direct_message = DirectMessage.find(params[:id])
end
private
def direct_message_params
params.require(:direct_message).permit(:title, :body)
end
def parsed_params
direct_message_params.merge(sender: @sender, receiver: @receiver)
end
end

View File

@@ -50,6 +50,15 @@ class Mailer < ApplicationMailer
end end
end end
def direct_message(direct_message)
@direct_message = direct_message
@receiver = @direct_message.receiver
with_user(@receiver) do
mail(to: @receiver.email, subject: "Has recibido un nuevo mensaje privado")
end
end
private private
def with_user(user, &block) def with_user(user, &block)

View File

@@ -0,0 +1,4 @@
class DirectMessage < ActiveRecord::Base
belongs_to :sender, class_name: 'User', foreign_key: 'sender_id'
belongs_to :receiver, class_name: 'User', foreign_key: 'receiver_id'
end

View File

@@ -23,6 +23,7 @@ class User < ActiveRecord::Base
has_many :spending_proposals, foreign_key: :author_id has_many :spending_proposals, foreign_key: :author_id
has_many :failed_census_calls has_many :failed_census_calls
has_many :notifications has_many :notifications
has_many :direct_messages
belongs_to :geozone belongs_to :geozone
validates :username, presence: true, if: :username_required? validates :username, presence: true, if: :username_required?

View File

@@ -0,0 +1,27 @@
<div class="row">
<div class="small-12 medium-9 column">
<%= render 'shared/back_link' %>
<h1><%= t("proposal_notifications.new.title") %></h1>
<div class="callout primary">
<p>
Este mensaje se enviará al usuario <%= @receiver.name %>
</p>
</div>
<%= form_for [@receiver, @direct_message] do |f| %>
<%= render "shared/errors", resource: @direct_message %>
<%= f.label :title, t("proposal_notifications.new.title_label") %>
<%= f.text_field :title, label: false %>
<%= f.label :body, t("proposal_notifications.new.body_label") %>
<%= f.text_area :body, label: false, rows: "3" %>
<div class="small-12 medium-4">
<%= f.submit t("proposal_notifications.new.submit_button"), class: "button expanded" %>
</div>
<% end %>
</div>
</div>

View File

@@ -0,0 +1,5 @@
<div>
<p><%= @direct_message.title %></p>
<p><%= @direct_message.body %></p>
<p><%= @direct_message.receiver.name %></p>
</div>

View File

@@ -0,0 +1,9 @@
<td style="padding-bottom: 20px; padding-left: 10px;">
<h1 style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;">
<%= @direct_message.title %>
</h1>
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= @direct_message.body %>
</p>
</td>

View File

@@ -10,6 +10,8 @@
<% end %> <% end %>
</h2> </h2>
<p><%= link_to "Enviar un mensaje privado", new_user_direct_message_path(@user) %></p>
<% if @user.public_activity || @authorized_current_user %> <% if @user.public_activity || @authorized_current_user %>
<ul class="menu simple margin-top"> <ul class="menu simple margin-top">
<% @valid_filters.each do |filter| %> <% @valid_filters.each do |filter| %>

View File

@@ -5,6 +5,7 @@ en:
create: create:
notice: "%{resource_name} created successfully." notice: "%{resource_name} created successfully."
debate: "Debate created successfully." debate: "Debate created successfully."
direct_message: "You message has been sent successfully."
proposal: "Proposal created successfully." proposal: "Proposal created successfully."
proposal_notification: "Your message has been sent correctly." proposal_notification: "Your message has been sent correctly."
spending_proposal: "Spending proposal created successfully. You can access it from %{activity}" spending_proposal: "Spending proposal created successfully. You can access it from %{activity}"

View File

@@ -5,6 +5,7 @@ es:
create: create:
notice: "%{resource_name} creado correctamente." notice: "%{resource_name} creado correctamente."
debate: "Debate creado correctamente." debate: "Debate creado correctamente."
direct_message: "Tu mensaje ha sido enviado correctamente."
proposal: "Propuesta creada correctamente." proposal: "Propuesta creada correctamente."
proposal_notification: "Tu message ha sido enviado correctamente." proposal_notification: "Tu message ha sido enviado correctamente."
spending_proposal: "Propuesta de inversión creada correctamente. Puedes acceder a ella desde %{activity}" spending_proposal: "Propuesta de inversión creada correctamente. Puedes acceder a ella desde %{activity}"

View File

@@ -83,7 +83,9 @@ Rails.application.routes.draw do
get :search, on: :collection get :search, on: :collection
end end
resources :users, only: [:show] resources :users, only: [:show] do
resources :direct_messages, only: [:new, :create, :show]
end
resource :account, controller: "account", only: [:show, :update, :delete] do resource :account, controller: "account", only: [:show, :update, :delete] do
get :erase, on: :collection get :erase, on: :collection

View File

@@ -0,0 +1,12 @@
class CreateDirectMessages < ActiveRecord::Migration
def change
create_table :direct_messages do |t|
t.integer :sender_id
t.integer :receiver_id
t.string :title
t.text :body
t.timestamps null: false
end
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160606102427) do ActiveRecord::Schema.define(version: 20160608174104) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -168,6 +168,15 @@ ActiveRecord::Schema.define(version: 20160606102427) do
add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
create_table "direct_messages", force: :cascade do |t|
t.integer "sender_id"
t.integer "receiver_id"
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "failed_census_calls", force: :cascade do |t| create_table "failed_census_calls", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.string "document_number" t.string "document_number"