stores a proposal notification

This commit is contained in:
rgarcia
2016-06-01 13:30:44 +02:00
parent 1975018d6a
commit f5375e813d
14 changed files with 155 additions and 4 deletions

View File

@@ -0,0 +1,29 @@
class ProposalNotificationsController < ApplicationController
skip_authorization_check
def new
@notification = ProposalNotification.new
@proposal = Proposal.find(params[:proposal_id])
end
def create
@notification = ProposalNotification.new(notification_params)
@proposal = Proposal.find(notification_params[:proposal_id])
if @notification.save
redirect_to @notification, notice: I18n.t("flash.actions.create.proposal_notification")
else
render :new
end
end
def show
@notification = ProposalNotification.find(params[:id])
end
private
def notification_params
params.require(:proposal_notification).permit(:title, :body, :proposal_id)
end
end

View File

@@ -0,0 +1,8 @@
class ProposalNotification < ActiveRecord::Base
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
belongs_to :proposal
validates :title, presence: true
validates :body, presence: true
validates :proposal, presence: true
end

View File

@@ -0,0 +1,13 @@
<%= form_for @notification do |f| %>
<%= render "shared/errors", resource: @notification %>
<%= 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 %>
<%= f.hidden_field :proposal_id, value: @proposal.id %>
<%= f.submit t("proposal_notifications.new.submit_button") %>
<% end %>

View File

@@ -0,0 +1,2 @@
<div><%= @notification.title %></div>
<div><%= @notification.body %></div>

View File

@@ -146,6 +146,7 @@ en:
not_saved: 'prevented this %{resource} from being saved:' not_saved: 'prevented this %{resource} from being saved:'
policy: Privacy Policy policy: Privacy Policy
proposal: Proposal proposal: Proposal
proposal_notification: "Notification"
spending_proposal: Spending proposal spending_proposal: Spending proposal
user: Account user: Account
verification/sms: phone verification/sms: phone
@@ -360,6 +361,11 @@ en:
update: update:
form: form:
submit_button: Save changes submit_button: Save changes
proposal_notifications:
new:
title_label: "Title"
body_label: "Message"
submit_button: "Send"
shared: shared:
advanced_search: advanced_search:
author_type: 'By author category' author_type: 'By author category'

View File

@@ -146,6 +146,7 @@ es:
not_saved: 'impidieron guardar %{resource}:' not_saved: 'impidieron guardar %{resource}:'
policy: Política de privacidad policy: Política de privacidad
proposal: la propuesta proposal: la propuesta
proposal_notification: "la notificación"
spending_proposal: la propuesta de gasto spending_proposal: la propuesta de gasto
user: la cuenta user: la cuenta
verification/sms: el teléfono verification/sms: el teléfono
@@ -360,6 +361,11 @@ es:
update: update:
form: form:
submit_button: Guardar cambios submit_button: Guardar cambios
proposal_notifications:
new:
title_label: "Título"
body_label: "Mensaje"
submit_button: "Enviar"
shared: shared:
advanced_search: advanced_search:
author_type: 'Por categoría de autor' author_type: 'Por categoría de autor'

View File

@@ -6,7 +6,9 @@ en:
notice: "%{resource_name} created successfully." notice: "%{resource_name} created successfully."
debate: "Debate created successfully." debate: "Debate created successfully."
proposal: "Proposal created successfully." proposal: "Proposal created successfully."
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}"
save_changes: save_changes:
notice: Changes saved notice: Changes saved
update: update:

View File

@@ -6,6 +6,7 @@ es:
notice: "%{resource_name} creado correctamente." notice: "%{resource_name} creado correctamente."
debate: "Debate creado correctamente." debate: "Debate creado correctamente."
proposal: "Propuesta creada correctamente." proposal: "Propuesta creada 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}"
save_changes: save_changes:
notice: Cambios guardados notice: Cambios guardados

View File

@@ -93,6 +93,8 @@ Rails.application.routes.draw do
put :mark_all_as_read, on: :collection put :mark_all_as_read, on: :collection
end end
resources :proposal_notifications, only: [:new, :create, :show]
resource :verification, controller: "verification", only: [:show] resource :verification, controller: "verification", only: [:show]
scope module: :verification do scope module: :verification do

View File

@@ -0,0 +1,12 @@
class CreateProposalNotifications < ActiveRecord::Migration
def change
create_table :proposal_notifications do |t|
t.string :title
t.text :body
t.integer :author_id
t.integer :proposal_id
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: 20160518141543) do ActiveRecord::Schema.define(version: 20160601103338) 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"
@@ -260,6 +260,15 @@ ActiveRecord::Schema.define(version: 20160518141543) do
add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree
create_table "proposal_notifications", force: :cascade do |t|
t.string "title"
t.text "body"
t.integer "author_id"
t.integer "proposal_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "proposals", force: :cascade do |t| create_table "proposals", force: :cascade do |t|
t.string "title", limit: 80 t.string "title", limit: 80
t.text "description" t.text "description"

View File

@@ -1,5 +1,4 @@
FactoryGirl.define do FactoryGirl.define do
sequence(:document_number) { |n| "#{n.to_s.rjust(8, '0')}X" } sequence(:document_number) { |n| "#{n.to_s.rjust(8, '0')}X" }
factory :user do factory :user do
@@ -326,4 +325,10 @@ FactoryGirl.define do
post_started_at Time.now - 7.days post_started_at Time.now - 7.days
post_ended_at Time.now + 7.days post_ended_at Time.now + 7.days
end end
factory :proposal_notification do
title "Thank you for supporting my proposal"
body "Please let others know so we can make it happen"
proposal
end
end end

View File

@@ -0,0 +1,31 @@
require 'rails_helper'
feature 'Proposal Notifications' do
scenario "Send a notification" do
noelia = create(:user)
vega = create(:user)
proposal = create(:proposal)
#use correct path from my activity
visit new_proposal_notification_path(proposal_id: proposal.id)
fill_in 'proposal_notification_title', with: "Thank you for supporting my proposal"
fill_in 'proposal_notification_body', with: "Please share it with others so we can make it happen!"
click_button "Send"
expect(page).to have_content "Your message has been sent correctly."
expect(page).to have_content "Thank you for supporting my proposal"
expect(page).to have_content "Please share it with others so we can make it happen!"
end
scenario "Error messages" do
proposal = create(:proposal)
visit new_proposal_notification_path(proposal_id: proposal.id)
click_button "Send"
expect(page).to have_content error_message
end
end

View File

@@ -0,0 +1,25 @@
require 'rails_helper'
describe ProposalNotification do
let(:notification) { build(:proposal_notification) }
it "should be valid" do
expect(notification).to be_valid
end
it "should not be valid without a title" do
notification.title = nil
expect(notification).to_not be_valid
end
it "should not be valid without a body" do
notification.body = nil
expect(notification).to_not be_valid
end
it "should not be valid without an associated proposal" do
notification.proposal = nil
expect(notification).to_not be_valid
end
end