diff --git a/Gemfile b/Gemfile index 1304933..84249ac 100644 --- a/Gemfile +++ b/Gemfile @@ -64,6 +64,7 @@ gem 'devise_invitable' # Use bootstrap to generate quick, elegent and responsive layouts gem 'bootstrap-sass' +gem 'bootstrap-datepicker-rails' # The following gems for testing purpose in development and testing environment group :development, :test do @@ -80,3 +81,10 @@ group :development, :test do end gem 'simple_form' +# Use to add redactor editor +gem 'redactor-rails' +gem 'carrierwave' +gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid' +gem 'mini_magick' +gem 'redis-rails' +gem 'redis-namespace' diff --git a/Gemfile.lock b/Gemfile.lock index a16b62d..50d1e1b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,10 +29,21 @@ GEM tzinfo (~> 1.1) arel (5.0.1.20140414130214) bcrypt (3.1.7) + bootstrap-datepicker-rails (1.3.0.2) + railties (>= 3.0) bootstrap-sass (3.2.0.0) sass (~> 3.2) bson (2.3.0) builder (3.2.2) + carrierwave (0.10.0) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) + mime-types (>= 1.16) + carrierwave-mongoid (0.7.1) + carrierwave (>= 0.8.0, < 0.11.0) + mongoid (>= 3.0, < 5.0) + mongoid-grid_fs (>= 1.3, < 3.0) celluloid (0.15.2) timers (~> 1.1.0) coffee-rails (4.0.1) @@ -97,6 +108,8 @@ GEM mime-types (~> 1.16) treetop (~> 1.4.8) mime-types (1.25.1) + mini_magick (3.7.0) + subexec (~> 0.2.1) mini_portile (0.6.0) minitest (5.3.5) mongoid (4.0.0) @@ -104,6 +117,9 @@ GEM moped (~> 2.0.0) origin (~> 2.1) tzinfo (>= 0.3.37) + mongoid-grid_fs (2.1.0) + mime-types (>= 1.0, < 3.0) + mongoid (>= 3.0, < 5.0) moped (2.0.0) bson (~> 2.2) connection_pool (~> 2.0) @@ -145,9 +161,29 @@ GEM rake (10.3.2) rdoc (4.1.1) json (~> 1.4) + redactor-rails (0.4.5) + devise + mime-types + orm_adapter redis (3.1.0) + redis-actionpack (4.0.0) + actionpack (~> 4) + redis-rack (~> 1.5.0) + redis-store (~> 1.1.0) + redis-activesupport (4.0.0) + activesupport (~> 4) + redis-store (~> 1.1.0) redis-namespace (1.5.0) redis (~> 3.0, >= 3.0.4) + redis-rack (1.5.0) + rack (~> 1.5) + redis-store (~> 1.1.0) + redis-rails (4.0.0) + redis-actionpack (~> 4) + redis-activesupport (~> 4) + redis-store (~> 1.1.0) + redis-store (1.1.4) + redis (>= 2.2) ref (1.0.5) rspec-core (3.0.2) rspec-support (~> 3.0.0) @@ -201,6 +237,7 @@ GEM actionpack (>= 3.0) activesupport (>= 3.0) sprockets (~> 2.8) + subexec (0.2.3) therubyracer (0.12.1) libv8 (~> 3.16.14.0) ref @@ -225,7 +262,10 @@ PLATFORMS ruby DEPENDENCIES + bootstrap-datepicker-rails bootstrap-sass + carrierwave + carrierwave-mongoid coffee-rails (~> 4.0.0) database_cleaner devise @@ -238,8 +278,12 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails linkedin + mini_magick mongoid rails (= 4.1.2) + redactor-rails + redis-namespace + redis-rails rspec-rails sass-rails (~> 4.0.3) sdoc (~> 0.4.0) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 86c1238..3eb87ad 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -14,6 +14,10 @@ //= require jquery_ujs //= require turbolinks //= require bootstrap +//= require bootstrap-datepicker +// require bootstrap-datepicker-rails +//= require redactor-rails +//= require redactor-rails/plugins //= require_tree . diff --git a/app/assets/javascripts/users.js.coffee b/app/assets/javascripts/users.js.coffee index 24f83d1..1c80829 100644 --- a/app/assets/javascripts/users.js.coffee +++ b/app/assets/javascripts/users.js.coffee @@ -1,3 +1,4 @@ # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://coffeescript.org/ +$('.datepicker').datepicker() diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 1cded50..958e1f5 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -12,6 +12,9 @@ * *= require_tree . *= require_self + *= require bootstrap-datepicker + *= require redactor-rails + *= redactor-rails/plugins */ @import "bootstrap"; diff --git a/app/assets/stylesheets/newsletters.css.scss b/app/assets/stylesheets/newsletters.css.scss new file mode 100644 index 0000000..f755752 --- /dev/null +++ b/app/assets/stylesheets/newsletters.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the newsletters controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/newsletters_controller.rb b/app/controllers/newsletters_controller.rb index 8987a92..a5a95fb 100644 --- a/app/controllers/newsletters_controller.rb +++ b/app/controllers/newsletters_controller.rb @@ -1,2 +1,47 @@ class NewslettersController < ApplicationController + + def index + @newsletters = Newsletter.order_by([:sent_on, :asc]) + end + + def show + @newsletter = Newsletter.find(params[:id]) + end + + def new + @newsletter = Newsletter.new + end + + def newsletters_params + params.require(:newsletter).permit(:id, :content, :sent_on, :users_count) + end + + def create + @newsletter = Newsletter.new(newsletters_params) + if @newsletter.save + redirect_to newsletters_path + else + render action: 'new' + end + end + + def edit + @newsletter = Newsletter.find(params[:id]) + end + + def update + @newsletter = Newsletter.find(params[:id]) + if @newsletter.update_attributes(newsletters_params) + redirect_to newsletters_path + else + render action: 'edit' + end + end + + def destroy + @newsletter = Newsletter.find(params[:id]) + @newsletter.destroy + redirect_to newsletters_path + end + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 66a3c09..f8ac86e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -13,15 +13,36 @@ def new def create @user = User.new(users_params) + # @news = Newsletter.last + + #respond_to do |format| if @user.save - redirect_to users_path + redirect_to users_path + # UserMailer.welcome_message(@user,@news.content).deliver + # format.html { redirect_to(@user, notice: 'User was successfully created.')} + # format.json { render json: @user, status: :created, locaton: @user} else render action: 'new' + # format.html {render action: 'new'} + # format.json {render json: @user.errors, status: :unprocessable_entity } end + #end end + def subscribe + @user = User.find(params[:id]) + @user.update(is_subscribed: 'false') + end + + def sendmailer + #@user = User.where(is_subscribed: 'true') + #@news = Newsletter.last + HardWorker.perform_async() + redirect_to users_path + end + def users_params - params.require(:user).permit(:id, :email_id, :is_subscribed, :joined_on, :source) + params.require(:user).permit(:id, :email_id, :is_subscribed, :joined_on, :source, :username) end def edit diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb new file mode 100644 index 0000000..d72c76a --- /dev/null +++ b/app/mailers/user_mailer.rb @@ -0,0 +1,22 @@ +class UserMailer < ActionMailer::Base + include SendGrid + sendgrid_category :use_subject_lines + sendgrid_enable :ganalytics, :opentrack + sendgrid_unique_args :key1 => "value1", :key2 => "value2" + + def welcome_message(email, newsletter_content,id,name) + sendgrid_category "Welcome" + sendgrid_unique_args :key2 => "newvalue2", :key3 => "value3" + + content = ERB.new(newsletter_content) + object_id = id + #headers['X-SMTPAPI'] = { :to => user.pluck(:email_id) }.to_json + + mail( to: email, + category: "newuser", + subject: "Welcome #{name}", + body: content.result(binding), + content_type: "text/html" + ) + end +end diff --git a/app/models/redactor_rails/asset.rb b/app/models/redactor_rails/asset.rb new file mode 100644 index 0000000..fe059e4 --- /dev/null +++ b/app/models/redactor_rails/asset.rb @@ -0,0 +1,9 @@ +class RedactorRails::Asset + include Mongoid::Document + include Mongoid::Timestamps + + include RedactorRails::Orm::Mongoid::AssetBase + + delegate :url, :current_path, :size, :content_type, :filename, :to => :data + validates_presence_of :data +end diff --git a/app/models/redactor_rails/document.rb b/app/models/redactor_rails/document.rb new file mode 100644 index 0000000..f286f3a --- /dev/null +++ b/app/models/redactor_rails/document.rb @@ -0,0 +1,14 @@ +class RedactorRails::Document < RedactorRails::Asset + mount_uploader :data, RedactorRailsDocumentUploader, :mount_on => :data_file_name + + def url_content + url(:content) + end + + def thumb + # Could theoretically provide an icon set here + # to match against the extensions + # but for now it's nil to address the bug + nil + end +end diff --git a/app/models/redactor_rails/picture.rb b/app/models/redactor_rails/picture.rb new file mode 100644 index 0000000..9a6f812 --- /dev/null +++ b/app/models/redactor_rails/picture.rb @@ -0,0 +1,7 @@ +class RedactorRails::Picture < RedactorRails::Asset + mount_uploader :data, RedactorRailsPictureUploader, :mount_on => :data_file_name + + def url_content + url(:content) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 11bfd53..466e4f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,11 +2,13 @@ class User include Mongoid::Document field :email_id, type: String + field :username, type: String field :is_subscribed, type: Boolean field :joined_on, type: Date field :source, type: String validates :email_id, presence: true + validates :username, presence: true validates :is_subscribed, presence: true validates :joined_on, presence: true validates :source, presence: true diff --git a/app/uploaders/redactor_rails_document_uploader.rb b/app/uploaders/redactor_rails_document_uploader.rb new file mode 100644 index 0000000..33bf6a6 --- /dev/null +++ b/app/uploaders/redactor_rails_document_uploader.rb @@ -0,0 +1,15 @@ +# encoding: utf-8 +class RedactorRailsDocumentUploader < CarrierWave::Uploader::Base + include RedactorRails::Backend::CarrierWave + + # storage :fog + storage :file + + def store_dir + "system/redactor_assets/documents/#{model.id}" + end + + def extension_white_list + RedactorRails.document_file_types + end +end diff --git a/app/uploaders/redactor_rails_picture_uploader.rb b/app/uploaders/redactor_rails_picture_uploader.rb new file mode 100644 index 0000000..99aadba --- /dev/null +++ b/app/uploaders/redactor_rails_picture_uploader.rb @@ -0,0 +1,53 @@ +# encoding: utf-8 +class RedactorRailsPictureUploader < CarrierWave::Uploader::Base + include RedactorRails::Backend::CarrierWave + + # Include RMagick or ImageScience support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + # include CarrierWave::ImageScience + + # Choose what kind of storage to use for this uploader: + storage :file + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "system/redactor_assets/pictures/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + process :read_dimensions + + # Create different versions of your uploaded files: + version :thumb do + process :resize_to_fill => [118, 100] + end + + version :content do + process :resize_to_limit => [800, 800] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + RedactorRails.image_file_types + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end +end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 968ff69..87a0687 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -17,7 +17,7 @@ %a{href: "#{home_index_path}"} %h4 Home %li - %a{href: "#"} + %a{href: "#{newsletters_path}"} %h4 Newsletters %li %a{href: "#{new_user_path}"} @@ -25,6 +25,12 @@ %li %a{href: "#{users_path}"} %h4 All Users + %li + %a{href: "#{new_newsletter_path}"} + %h4 Add New Newsletter + %li + %a{href: "#{sendmailer_path}"} + %h4 Send Mail %ul.nav.navbar-nav.navbar-right diff --git a/app/views/newsletters/_form.html.haml b/app/views/newsletters/_form.html.haml new file mode 100644 index 0000000..71dc4d4 --- /dev/null +++ b/app/views/newsletters/_form.html.haml @@ -0,0 +1,6 @@ += simple_form_for (@newsletter) do |news| + .inputs + = news.text_area :content, :placeholder => "Input here..", :class => "redactor", :rows => 20, :cols => 60 + = news.input :sent_on + = news.input :users_count + = news.button :submit diff --git a/app/views/newsletters/edit.html.haml b/app/views/newsletters/edit.html.haml new file mode 100644 index 0000000..f3a2592 --- /dev/null +++ b/app/views/newsletters/edit.html.haml @@ -0,0 +1 @@ += render :partial => "form" diff --git a/app/views/newsletters/index.html.haml b/app/views/newsletters/index.html.haml new file mode 100644 index 0000000..89b9cb3 --- /dev/null +++ b/app/views/newsletters/index.html.haml @@ -0,0 +1,10 @@ +%table.table.table-striped + -@newsletters.each do |news| + %tr + %td + =link_to news.sent_on, newsletter_path(news) + %td + =link_to "Edit", edit_newsletter_path(news) + %td + =link_to "Delete", newsletter_path(news), method: :delete + %tr diff --git a/app/views/newsletters/new.html.haml b/app/views/newsletters/new.html.haml new file mode 100644 index 0000000..f3a2592 --- /dev/null +++ b/app/views/newsletters/new.html.haml @@ -0,0 +1 @@ += render :partial => "form" diff --git a/app/views/newsletters/show.html.haml b/app/views/newsletters/show.html.haml new file mode 100644 index 0000000..1420583 --- /dev/null +++ b/app/views/newsletters/show.html.haml @@ -0,0 +1,2 @@ += raw @newsletter.content +=@newsletter.users_count diff --git a/app/views/user_mailer/welcome_message.html.haml b/app/views/user_mailer/welcome_message.html.haml new file mode 100644 index 0000000..8bb7818 --- /dev/null +++ b/app/views/user_mailer/welcome_message.html.haml @@ -0,0 +1,12 @@ +!!! +%html + %body + %p + { + "filters" : { + "template" : { + "settings" : 1, + "text/html" : " + = succeed "\"" do + %div{bgcolor: "#FFF", style: "width:200px"} + - body diff --git a/app/views/user_mailer/welcome_message.text.erb b/app/views/user_mailer/welcome_message.text.erb new file mode 100644 index 0000000..f72993e --- /dev/null +++ b/app/views/user_mailer/welcome_message.text.erb @@ -0,0 +1,4 @@ +Welcome To Lightair.com +============================================== +Josh Software Rocks +Thanks for the support and have a great day! diff --git a/app/views/users/_form.html.haml b/app/views/users/_form.html.haml index 9eae706..7af80d4 100644 --- a/app/views/users/_form.html.haml +++ b/app/views/users/_form.html.haml @@ -1,8 +1,15 @@ = simple_form_for (@user) do |f| .inputs = f.input :email_id - = f.input :is_subscribed - = f.input :joined_on + = f.input :username + = f.input :is_subscribed, :as => :select + = f.input :joined_on, input_html: { 'data-behaviour' => 'datepicker'} = f.input :source = f.button :submit + :javascript + $('[data-behaviour~=datepicker]').datepicker({ + format: 'yyyy-mm-dd' + }) + + diff --git a/app/views/users/sendmailer.html.haml b/app/views/users/sendmailer.html.haml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/app/views/users/sendmailer.html.haml @@ -0,0 +1 @@ + diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml index 570f57f..ef2bfb9 100644 --- a/app/views/users/show.html.haml +++ b/app/views/users/show.html.haml @@ -1,3 +1,4 @@ +=@user.username =@user.source =@user.joined_on =@user.is_subscribed diff --git a/app/views/users/subscribe.html.haml b/app/views/users/subscribe.html.haml new file mode 100644 index 0000000..02394f2 --- /dev/null +++ b/app/views/users/subscribe.html.haml @@ -0,0 +1 @@ +%h2 Unsubscribed successfully diff --git a/app/workers/hard_worker.rb b/app/workers/hard_worker.rb new file mode 100644 index 0000000..c958f2c --- /dev/null +++ b/app/workers/hard_worker.rb @@ -0,0 +1,13 @@ +class HardWorker + include Sidekiq::Worker + + def perform() + @users = User.where(is_subscribed: "true") + @news = Newsletter.last + @users.each do |a| + UserMailer.welcome_message(a.email_id,@news.content,a.id,a.username).deliver + end + + end +end + diff --git a/config/application.rb b/config/application.rb index 6e89af7..d21154e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -28,5 +28,6 @@ class Application < Rails::Application # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif) + #config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expires_in: 90.minutes } end end diff --git a/config/environments/development.rb b/config/environments/development.rb index a384d95..4155830 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -14,7 +14,7 @@ config.action_controller.perform_caching = false # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false + config.action_mailer.raise_delivery_errors = true # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log @@ -31,4 +31,16 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + config.action_mailer.perform_deliveries = true + config.action_mailer.default_options = {from: 'marketing@joshsoftware.com'} + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = { + :address => "smtp.sendgrid.net", + :port => 25, + :domain => "sendgrid.me", + :authentication => :plain, + :user_name => "gautamrege", + :password => "grege123" + } + config.action_mailer.default_url_options = { host: 'localhost:3000' } end diff --git a/config/environments/production.rb b/config/environments/production.rb index 4f67ce3..56b7477 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -68,6 +68,7 @@ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = true + config.action_mailer.default_url_options = { host: 'example.com' } # Send deprecation notices to registered listeners. config.active_support.deprecation = :notify diff --git a/config/environments/test.rb b/config/environments/test.rb index 053f5b6..6278aaa 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -36,4 +36,5 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + config.action_mailer.default_options = {from: 'marketing@joshsoftware.com'} end diff --git a/config/initializers/redis.rb b/config/initializers/redis.rb new file mode 100644 index 0000000..679afed --- /dev/null +++ b/config/initializers/redis.rb @@ -0,0 +1 @@ +$redis = Redis::Namespace.new("lightair", :redis => Redis.new) diff --git a/config/initializers/session_store.rb b/config/initializers/session_store.rb index f799bd4..69e3b99 100644 --- a/config/initializers/session_store.rb +++ b/config/initializers/session_store.rb @@ -1,3 +1,4 @@ # Be sure to restart your server when you modify this file. Rails.application.config.session_store :cookie_store, key: '_lightair_session' +#MyApplication::Application.config.session_store :redis_store diff --git a/config/routes.rb b/config/routes.rb index 87b8e4e..2ad37da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,11 @@ Rails.application.routes.draw do + mount RedactorRails::Engine => '/redactor_rails' + get '/users/sendmailer', to: 'users#sendmailer', as: 'sendmailer' + get '/users/subscribe', to: 'users#subscribe', as: 'subscribe' resources :users resources :newsletters resources :home + resources :user_mailer root :to => "home#index" # The priority is based upon order of creation: first created -> highest priority. diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 0000000..56af04e --- /dev/null +++ b/dump.rdb @@ -0,0 +1 @@ +REDIS0006ÿܳCðZÜòV \ No newline at end of file diff --git a/spec/controllers/newsletters_controller_spec.rb b/spec/controllers/newsletters_controller_spec.rb index 0909932..712a05a 100644 --- a/spec/controllers/newsletters_controller_spec.rb +++ b/spec/controllers/newsletters_controller_spec.rb @@ -2,4 +2,68 @@ RSpec.describe NewslettersController, :type => :controller do + context "GET index" do + it "list all the newsletters" do + get :index + expect(response).to render_template("index") + end + end + + context "GET show" do + let(:newsletter) {FactoryGirl.create(:newsletter)} + it "show the deatails of the newsletters" do + get :show, {:id => newsletter.id} + expect(response).to render_template("show") + end + end + + context "GET new" do + let(:new_newsletter) {FactoryGirl.create(:newsletter)} + it "display new form for adding newsletter" do + post :new, {newsletter: new_newsletter} + expect(response).to render_template("new") + end + end + + context "POST create" do + let(:new_newsletter) {FactoryGirl.attributes_for(:newsletter)} + it "display new form for adding newsletter" do + post :create, {newsletter: new_newsletter} + expect(response).to redirect_to(newsletters_path) + end + + it "not arise" do + post :create, {newsletter: {content: "",sent_on: "2014-1-1", users_count: 0}} + expect(response).to render_template("new") + end + end + + context "GET update" do + let(:new_newsletter) {FactoryGirl.create(:newsletter)} + it "fetches the specific newsletter" do + get :edit, {id: new_newsletter.id} + expect(response).to render_template("edit") + end + end + + context "DELETE delete" do + let(:newsletter) {FactoryGirl.create(:newsletter)} + it "delete a newsletter" do + delete :destroy, {id: newsletter.id} + expect(response).to redirect_to(newsletters_path) + end + end + + context "PUT update" do + let(:new_newsletter) {FactoryGirl.create(:newsletter)} + it "updates details of specified newsletter" do + put :update, {id: new_newsletter.id, newsletter: {content: new_newsletter.content}} + expect(response).to redirect_to(newsletters_path) + end + + it "not arise" do + put :update, {id: new_newsletter.id, newsletter: {content: ""}} + expect(response).to render_template("edit") + end + end end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index d7bec43..3d321e7 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -12,7 +12,7 @@ context "GET show" do let(:user) {FactoryGirl.create(:user)} it "show the details of the users" do - get :show, {:id => user.id} + get :show, {id: user.id} expect(response).to render_template("show") end end @@ -32,15 +32,15 @@ expect(response).to redirect_to(users_path) end it "not arise" do - post :create, {user: new_user} - expect(response).not_to render_template("new") + post :create, {user: {email_id: "",username: "kanhaiya", is_subscribed: "false"}} + expect(response).to render_template("new") end end context "GET edit" do let(:new_user) {FactoryGirl.create(:user)} it "fetches the specific all the users" do - get :edit, {:id => new_user.id} + get :edit, {id: new_user.id} expect(response).to render_template("edit") end end @@ -49,7 +49,7 @@ context "DELETE delete" do let(:user) {FactoryGirl.create(:user)} it "delete an user" do - delete :destroy, {:id => user.id} + delete :destroy, {id: user.id} expect(response).to redirect_to(users_path) end end @@ -58,12 +58,27 @@ context "PUT update" do let(:new_user) {FactoryGirl.create(:user)} it "updates details of specified user" do - patch :update, {:id =>new_user.id, :user => { :email_id => new_user.email_id}} + patch :update, {id: new_user.id, user: { email_id: new_user.email_id}} expect(response).to redirect_to(users_path) end it " not arise" do - put :update, {:id =>new_user.id, :user => { :email => new_user.email_id}} - expect(response).not_to render_template("edit") + put :update, {id: new_user.id, user: { email_id: ""}} + expect(response).to render_template("edit") + end + end + + context "GET subscribe" do + let(:new_user) {FactoryGirl.create(:user)} + it "unsubcribes a particular user" do + get :subscribe, {id: new_user.id} + expect(response).to render_template("subscribe") + end + end + + context "GET sendmailer" do + it "sends mails to user" do + get :sendmailer + expect(response).to redirect_to(users_path) end end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 36d4e31..71630d3 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -6,6 +6,7 @@ u.is_subscribed {true} u.joined_on {Faker::Business.credit_card_expiry_date} u.source {Faker::Name.name} + u.username {Faker::Name.name} newsletter end end diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb new file mode 100644 index 0000000..af37340 --- /dev/null +++ b/spec/mailers/user_mailer_spec.rb @@ -0,0 +1,30 @@ +require "rails_helper" + +RSpec.describe UserMailer, :type => :mailer do + pending "add some examples to (or delete) #{__FILE__}" + + before(:each) do + ActionMailer::Base.delivery_method = :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries = [] + UserMailer.welcome_message('kanhaiya@joshsoftware.com','Josh','123','kanhaiya').deliver + end + + after(:each) do + ActionMailer::Base.deliveries.clear + end + + + it 'should send an email' do + expect(ActionMailer::Base.deliveries.count).to eq(1) + end + + it 'renders the receiver email' do + expect(ActionMailer::Base.deliveries.first.to).to eq(['kanhaiya@joshsoftware.com']) + end + + it 'render the sender email' do + expect(ActionMailer::Base.deliveries.first.from).to eq(['marketing@joshsoftware.com']) + end + +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 48ab4c0..5cef3f5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -26,5 +26,6 @@ expect(k).not_to be_valid end - + let(:user) {FactoryGirl.create(:user)} + let(:newsletter) {FactoryGirl.create(:newsletter)} end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cfb18dc..b81d6eb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +require 'simplecov' +SimpleCov.start + # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause this