diff --git a/app/controllers/proposal_notifications_controller.rb b/app/controllers/proposal_notifications_controller.rb new file mode 100644 index 000000000..bd83c941c --- /dev/null +++ b/app/controllers/proposal_notifications_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/proposal_notification.rb b/app/models/proposal_notification.rb new file mode 100644 index 000000000..d31b2e1a5 --- /dev/null +++ b/app/models/proposal_notification.rb @@ -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 \ No newline at end of file diff --git a/app/views/proposal_notifications/new.html.erb b/app/views/proposal_notifications/new.html.erb new file mode 100644 index 000000000..728b022a1 --- /dev/null +++ b/app/views/proposal_notifications/new.html.erb @@ -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 %> \ No newline at end of file diff --git a/app/views/proposal_notifications/show.html.erb b/app/views/proposal_notifications/show.html.erb new file mode 100644 index 000000000..e3c50e8cc --- /dev/null +++ b/app/views/proposal_notifications/show.html.erb @@ -0,0 +1,2 @@ +
<%= @notification.title %>
+
<%= @notification.body %>
\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 62fc521c5..725e8d08b 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -146,6 +146,7 @@ en: not_saved: 'prevented this %{resource} from being saved:' policy: Privacy Policy proposal: Proposal + proposal_notification: "Notification" spending_proposal: Spending proposal user: Account verification/sms: phone @@ -360,6 +361,11 @@ en: update: form: submit_button: Save changes + proposal_notifications: + new: + title_label: "Title" + body_label: "Message" + submit_button: "Send" shared: advanced_search: author_type: 'By author category' diff --git a/config/locales/es.yml b/config/locales/es.yml index 05e53fc2b..840dcf705 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -146,6 +146,7 @@ es: not_saved: 'impidieron guardar %{resource}:' policy: Política de privacidad proposal: la propuesta + proposal_notification: "la notificación" spending_proposal: la propuesta de gasto user: la cuenta verification/sms: el teléfono @@ -360,6 +361,11 @@ es: update: form: submit_button: Guardar cambios + proposal_notifications: + new: + title_label: "Título" + body_label: "Mensaje" + submit_button: "Enviar" shared: advanced_search: author_type: 'Por categoría de autor' diff --git a/config/locales/responders.en.yml b/config/locales/responders.en.yml index 3f83f746a..1155ff34a 100755 --- a/config/locales/responders.en.yml +++ b/config/locales/responders.en.yml @@ -6,7 +6,9 @@ en: notice: "%{resource_name} created successfully." debate: "Debate 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}" + save_changes: notice: Changes saved update: diff --git a/config/locales/responders.es.yml b/config/locales/responders.es.yml index e57dacd31..948b6650f 100644 --- a/config/locales/responders.es.yml +++ b/config/locales/responders.es.yml @@ -6,6 +6,7 @@ es: notice: "%{resource_name} creado correctamente." debate: "Debate creado 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}" save_changes: notice: Cambios guardados diff --git a/config/routes.rb b/config/routes.rb index 7d0a3a996..6cba48e42 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -93,6 +93,8 @@ Rails.application.routes.draw do put :mark_all_as_read, on: :collection end + resources :proposal_notifications, only: [:new, :create, :show] + resource :verification, controller: "verification", only: [:show] scope module: :verification do diff --git a/db/migrate/20160601103338_create_proposal_notifications.rb b/db/migrate/20160601103338_create_proposal_notifications.rb new file mode 100644 index 000000000..97760da6b --- /dev/null +++ b/db/migrate/20160601103338_create_proposal_notifications.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 10f10c7a8..91628725a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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 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 + 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| t.string "title", limit: 80 t.text "description" diff --git a/spec/factories.rb b/spec/factories.rb index 62bea738f..0a9645dcb 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,5 +1,4 @@ FactoryGirl.define do - sequence(:document_number) { |n| "#{n.to_s.rjust(8, '0')}X" } factory :user do @@ -323,7 +322,13 @@ FactoryGirl.define do style {["banner-style-one", "banner-style-two", "banner-style-three"].sample} image {["banner.banner-img-one", "banner.banner-img-two", "banner.banner-img-three"].sample} target_url {["/proposals", "/debates" ].sample} - post_started_at Time.now - 7.days - post_ended_at Time.now + 7.days + post_started_at Time.now - 7.days + post_ended_at Time.now + 7.days + 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 diff --git a/spec/features/proposal_notifications_spec.rb b/spec/features/proposal_notifications_spec.rb new file mode 100644 index 000000000..756fc3fdd --- /dev/null +++ b/spec/features/proposal_notifications_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/models/proposal_notification_spec.rb b/spec/models/proposal_notification_spec.rb new file mode 100644 index 000000000..5a7575219 --- /dev/null +++ b/spec/models/proposal_notification_spec.rb @@ -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