Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send_mail_to_users #6

Merged
merged 24 commits into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ gem 'puma'
group :development do
gem "better_errors"
gem "binding_of_caller"
gem 'pry'

gem 'spring'
gem 'spring-commands-rspec'
Expand All @@ -67,6 +66,8 @@ group :test do
end

group :development, :test do
gem 'faker'
gem 'pry'
gem 'rspec-rails', '~> 3.2.1'
gem 'factory_girl_rails'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ GEM
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faker (1.4.3)
i18n (~> 0.5)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
fastercsv (1.5.5)
Expand Down Expand Up @@ -320,6 +322,7 @@ DEPENDENCIES
devise_token_auth (>= 0.1.32.beta9)
enumerize
factory_girl_rails
faker
figaro
font-awesome-sass
gon
Expand Down
33 changes: 29 additions & 4 deletions app/assets/javascripts/pages/errors.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,34 @@ PubSub.subscribe('assigned.website', (ev, website)->
$('#solve').on 'click', (e)->
e.preventDefault();
$.ajax
data: { status: 'resolved' }
url: Routes.error_url(gon.error_id)
data: {error: {status: 'resolved'}}
url: Routes.api_v1_error_url(gon.error_id)
type: 'PUT'
success: (result)->
debugger;
return
alert 'Status updated'
return

$('.messageTextarea').keydown((event) ->
if event.keyCode == 13
$('#notify').submit()
return false
return
).focus(->
if @value == ''
@value = ''
return
).blur ->
if @value == ''
@value = ''
return
$('form#notify').submit ->
dataString = $('.messageTextarea').val()
$.ajax
url: Routes.notify_subscribers_api_v1_error_url(gon.error_id)
type: 'POST'
data: {message: dataString}
success: (data) ->
# finish load
console.log data, dataString, 'fail'
return
false
2 changes: 1 addition & 1 deletion app/assets/javascripts/routes.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= JsRoutes.generate(default_url_options: {:host => ENV['APP_DOMAIN']}, url_links: true, exclude: [/admin/] ) %>
<%= JsRoutes.generate(default_url_options: { :host => 'localhost:3000' }, url_links: true, exclude: [/admin/] ) %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asta nu a mers sa lasi cu ENV?? De ce ai pus fix localhost:3000 ??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

te rog sa pui la loc ENV['APP_DOMAIN'] aici si o sa fac merge.

22 changes: 20 additions & 2 deletions app/controllers/api/v1/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
class Api::V1::ErrorsController < Api::V1::ApiController
skip_before_action :authenticate_member!
skip_before_action :authenticate_member!, except: [:index, :show, :update, :notify_subscribers]

def index
@errors = current_site.issues
end

def create
# binding.pry
end

def show
@error = current_site.issues.where('issues.id = ?', params[:id]).first
end

def update
@error = Issue.find(params[:id])
@error.update_attributes(error_params)
end

def notify_subscribers
@error = Issue.find(params[:id])
@message = params[:message]
@error.subscribers.each do |member|
UserMailer.issue_solved(@error, member, @message).deliver_now
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hai sa schimbam si numele asta sa ii zicem notify_subscriber si fai un IssueMailer si sa lasam UserMailer pt altceva.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deci fai IssueMailer.notify_subscriber

end
end


private
def error_params
params.require(:error).permit(:status)
end
end
2 changes: 1 addition & 1 deletion app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ErrorsController < ApplicationController
def index
end

def show
def show
gon.error_id = params[:id]
end
end
1 change: 0 additions & 1 deletion app/controllers/installations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class InstallationsController < ApplicationController
def index

end
end
4 changes: 4 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout 'mailer'
end
11 changes: 11 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UserMailer < ApplicationMailer

def issue_solved(issue, subscriber, message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notify_subscriber ar trebuii sa fie numele la metoda asta pentru ca o sa dam voie ownerilor sa dea mesaje subscriberilor de mai multe ori. Normal ca trebuie sa schimbi si specurile si controlleru.

@issue = issue
@message = message
@website = Website.find(@issue.website_id)
@member = subscriber
mail(to: @member.email, subject: 'Subscriber notification')
end

end
2 changes: 1 addition & 1 deletion app/models/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class Issue < ActiveRecord::Base
belongs_to :website
enumerize :status, in: {:unresolved => 1, :resolved => 2}, default: :unresolved


validates :description, :presence => true, length: {minimum: 10}

end
5 changes: 2 additions & 3 deletions app/models/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Subscriber < ActiveRecord::Base
belongs_to :website
has_and_belongs_to_many :issues, join_table: "subscriber_issues"

validates :name, presence: true
validates :email, presence: true, uniqueness: true
validates :website, :presence => true
validates_presence_of :name, :email, :website
validates_uniqueness_of :email, scope: :website_id
end
1 change: 1 addition & 0 deletions app/views/api/v1/errors/notify_subscribers.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(@error, :id)
1 change: 1 addition & 0 deletions app/views/api/v1/errors/update.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.error(@error, :status)
6 changes: 3 additions & 3 deletions app/views/errors/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
.panel.panel-default
.panel-heading Update
.panel-body
%form
%form#notify
.form-group
%label.subscribers_count{:for => "subscriber_update"}
%textarea#subscriber_update.form-control{:rows => "3"}
%textarea#subscriber_update.form-control.messageTextarea{:rows => "3"}
.row
.col-md-9.col-xs-7
.checkbox
%label
%input{:type => "checkbox", :value => ""}/
Notify users by email
.col-md-3.col-xs-5
%button.btn.btn-primary.pull-right
%button.btn.btn-primary.pull-right#subscriber_update
%span.fa.fa-paper-plane
Send
.panel.panel-default
Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/mailer.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%hmtl
%body
= yield
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= yield
5 changes: 5 additions & 0 deletions app/views/user_mailer/issue_solved.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Welcome, #{@member.name}
%p An issue has just been solved.
%p Website title: #{@website.title}
%p #{@message}
%p Thanks for joining and have a great day!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schimba mailu asta sa fie asa:

Hi, #{@member.name},

%p #{@message}

%p This email was sent because you subscribed to an error on our website.

6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
}
namespace :api, defaults: {format: :json} do
namespace :v1 do
resources :errors, only: [:create, :index, :show]
resources :errors, only: [:create, :index, :show, :update] do
member do
post :notify_subscribers
end
end
resources :subscribers, only: [:index]
resources :members, only: [:show]
resources :websites, only: [:index]
Expand Down
1 change: 1 addition & 0 deletions dump.rdb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REDIS0006�ܳC�Z��V
107 changes: 107 additions & 0 deletions spec/controllers/api/v1/errors_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require 'rails_helper'
include Devise::TestHelpers


describe Api::V1::ErrorsController, :type => :controller do
let(:member) { create :member }
let(:website) { create :website, member: member }
let(:issue_error) { create :issue, website: website }
let(:subscriber) { create :subscriber, website: website }
let!(:issue_subscriber) { create :subscriber_issue, issue: issue_error, subscriber: subscriber }
let(:message) { 'asdada' }

describe 'POST #notify_subscribers' do
before { auth_member(member) }

it 'should email subscribers' do
mailer = double('UserMailer')
expect(mailer).to receive(:deliver_now)
expect(UserMailer).to receive(:issue_solved).with(issue_error, subscriber, message).and_return(mailer).once

post :notify_subscribers, { message: message, id: issue_error.id, format: :json }
end

it 'should email 2 subscribers' do
subscriber2 = create :subscriber, website: website
subscriber_issue = create :subscriber_issue, issue: issue_error, subscriber: subscriber2
mailer = double('UserMailer')
expect(mailer).to receive(:deliver_now).twice
expect(UserMailer).to receive(:issue_solved).with(issue_error, an_instance_of(Subscriber), message).and_return(mailer).twice
post :notify_subscribers, { message: message, id: issue_error.id, format: :json }
end

it 'should assign error' do
post :notify_subscribers, { id: issue_error.id, error: {status: issue_error.status }, format: :json }
expect(assigns(:error)).to eq(issue_error)
end

it 'should assign message' do
post :notify_subscribers, { message: 'asdada', id: issue_error.id, format: :json }
expect(assigns(:message)).to eq('asdada')
end
end

describe 'GET #index' do
it 'should assign current_site errors' do
auth_member(member)
get :index, { website_id: website.id, format: :json}
expect(assigns(:errors)).to eq([issue_error])
end

it 'should give error if not logged in' do
get :index, { website_id: website.id, format: :json}
expect(response.body).to eq({errors: ['Authorized users only.']}.to_json)
expect(response).to have_http_status(401)
end

it 'should render json' do
auth_member(member)
get :index, { website_id: website.id, format: :json}
expect(response).to be_successful
expect(response.content_type).to eq('application/json')
end
end

describe 'GET #show' do
it 'should assign error' do
auth_member(member)
get :show, { id: issue_error.id, error: {status: issue_error.status }, website_id: website.id, format: :json }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aici nu ai nevoie de error: {status: issue_error.status} pt ca nu faci update doar vrei sa iei issue. la fel si la testu de jos

expect(assigns(:error)).to eq(issue_error)
end

it 'should render json' do
auth_member(member)
get :show, { id: issue_error.id, error: {status: issue_error.status }, website_id: website.id, format: :json }
expect(response).to be_successful
expect(response.content_type).to eq('application/json')
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adauga un test la fel ca ala de la index fara auth_member. Codu tre sa fie exact la fel doar ca faci get :show in loc de get :index si ma rog cu id.

end

describe 'PUT #update' do
it 'should assign error' do
auth_member(member)
put :update, { id: issue_error.id, error: {status: issue_error.status }, format: :json }
expect(assigns(:error)).to eq(issue_error)
end

it 'should update error status' do
auth_member(member)
expect {
put :update, { id: issue_error.id, error: { status: "resolved" }, website_id: website.id, format: :json}
issue_error.reload
}.to change(issue_error, :status).from('unresolved').to('resolved')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aici ai un spatiu in fata la linia asta }.to tre sa fie pe verticala la fel ca expect

end

it 'should not allow update of other parameters other than status' do
auth_member(member)
expect{ put :update, { id: issue_error.id, error: { error: issue_error.status }, website: website.id, format: :json }}.to_not change(issue_error, :status).from("unresolved")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aici te rog sa identezi cum trebuie linia asta.

end

it 'should render json' do
auth_member(member)
put :update, { id: issue_error.id, error: { status: issue_error.status }, format: :json }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aici schimba issue_error.status cu resolved nu trimiti niciodata acelasi status nu are sens e dubios de ciudat.

expect(response).to be_successful
expect(response.content_type).to eq('application/json')
end
end
end
3 changes: 2 additions & 1 deletion spec/factories/issues.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryGirl.define do
factory :issue do
page_title "Homepage"
description "test description for error"
association :website
end

end
2 changes: 1 addition & 1 deletion spec/factories/members.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryGirl.define do
factory :member do
email "hello@google.com"
sequence(:email) { |n| "person#{n}@example.com" }
name "Test User 1"
provider "email"
confirmed_at Time.now
Expand Down
6 changes: 2 additions & 4 deletions spec/factories/subscribers.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
FactoryGirl.define do
factory :subscriber do
name "Test User 1"
email "testuser@google.com"
name Faker::Name.name
sequence(:email) { |n| "person#{n}@example.com" }
association :website

issues {[FactoryGirl.create(:issue)]}
end

end
4 changes: 4 additions & 0 deletions spec/mailers/previews/user_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview

end
5 changes: 5 additions & 0 deletions spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe UserMailer, type: :mailer do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading