Skip to content

Commit

Permalink
User edit form changes
Browse files Browse the repository at this point in the history
- Added few fields to user edit form
- Updated some fields as readonly
- Modified the params for newly added fields
- Added constant for different user status
  • Loading branch information
tejaszambre authored and Ashish944 committed Jan 14, 2021
1 parent 4d3bea6 commit aafa06f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 42 deletions.
43 changes: 26 additions & 17 deletions app/controllers/light/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ def sendtest
end

def unsubscribe
if @user.present? && @user.sidekiq_status == 'Subscribed'
@user.update(is_subscribed: 'false',
unsubscribed_at: DateTime.now,
sidekiq_status: 'Unsubscribed')
if @user.present? && @user.sidekiq_status == Light::User::STATUS[:subscribed]
@user.update(
is_subscribed: false,
unsubscribed_at: DateTime.now,
sidekiq_status: Light::User::STATUS[:unsubscribed]
)
@message = 'Unsubscribed successfully!!'
else
@message = response_message('unsubscribed')
end
end

def subscribe
if @user.present? && @user.sidekiq_status == 'Unsubscribed'
@user.update(is_subscribed: 'true',
sidekiq_status: 'Subscribed',
if @user.present? && @user.sidekiq_status == Light::User::STATUS[:unsubscribed]
@user.update(is_subscribed: true,
sidekiq_status: Light::User::STATUS[:subscribed],
subscribed_at: DateTime.now,
remote_ip: request.remote_ip,
user_agent: request.env['HTTP_USER_AGENT'])
Expand Down Expand Up @@ -116,16 +118,20 @@ def auto_opt_in
def opt_in
@user = Light::User.where(email_id: params[:email]).first
if @user.present?
@user.update_attributes(is_subscribed: true,
sidekiq_status: 'Subscribed',
subscribed_at: DateTime.now)
@user.update_attributes(
is_subscribed: true,
sidekiq_status: Light::User::STATUS[:subscribed],
subscribed_at: DateTime.now
)
else
u_name = params[:username].blank? ? params[:email] : params[:username]
@user = Light::User.new(username: u_name,
email_id: params[:email],
source: 'web subscription request',
subscribed_at: DateTime.now,
sidekiq_status: 'Subscribed')
@user = Light::User.new(
username: u_name,
email_id: params[:email],
source: 'web subscription request',
subscribed_at: DateTime.now,
sidekiq_status: Light::User::STATUS[:subscribed]
)
end
respond_to do |format|
format.json { head :no_content }
Expand All @@ -139,7 +145,10 @@ def thank_you

private
def users_params
params.require(:user).permit(:id, :email_id, :is_subscribed, :joined_on, :source, :username)
params.require(:user).permit(
:id, :email_id, :is_subscribed, :joined_on, :is_blocked,
:sidekiq_status, :source, :username
)
end

def user_with_token
Expand All @@ -154,7 +163,7 @@ def response_message(status)
if dummy_token?
"#{status.capitalize} successfully!!"
elsif @user.nil?
"Hey, it seems request you are trying to access is invalid. If you have any " +
"Hey, it seems request you are trying to access is invalid. If you have any " +
"concerns about our newsletter's subscription, kindly get in touch with " +
"<a href='mailto:[email protected]' class='email'>[email protected]</a>"
else
Expand Down
24 changes: 14 additions & 10 deletions app/models/light/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ class User
include Mongoid::Slug
include Mongoid::History::Trackable

NEW_USER = "new user"
STATUS = {
new_user: 'new_user', subscribed: 'Subscribed', unsubscribed: 'Unsubscribed',
block: 'Block', bounced: 'Bounced', spam: 'Spam', invalid: 'Invalid',
opt_in: 'Opt in mail sent'
}

field :email_id, type: String
field :username, type: String
Expand Down Expand Up @@ -32,21 +36,21 @@ class User
:user_agent, :is_subscribed, :unsubscribed_at]
before_create do
self.joined_on = Date.today
self.sidekiq_status = NEW_USER if self.sidekiq_status.blank?
self.sidekiq_status = STATUS[:new_user] if self.sidekiq_status.blank?
self.token = Devise.friendly_token
while User.where(token: self.token).present?
self.token = Devise.friendly_token
end
end

scope :subscribed_users, -> { where(is_subscribed: true, sidekiq_status: 'Subscribed') }
scope :unsubscribed_users, -> { where(is_subscribed: false, sidekiq_status: 'Unsubscribed') }
scope :new_users, -> { where(is_subscribed: false, sidekiq_status: NEW_USER) }
scope :blocked_users, -> { where(is_subscribed: false, sidekiq_status: 'Block') }
scope :bounced_users, -> { where(is_subscribed: false, sidekiq_status: 'Bounced') }
scope :spam_users, -> { where(is_subscribed: false, sidekiq_status: 'Spam') }
scope :invalid_users, -> { where(is_subscribed: false, sidekiq_status: 'Invalid') }
scope :opt_in_users, -> { where(is_subscribed: false, sidekiq_status: 'Opt in mail sent') }
scope :subscribed_users, -> { where(is_subscribed: true, sidekiq_status: STATUS[:subscribed]) }
scope :unsubscribed_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:unsubscribed]) }
scope :new_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:new_user]) }
scope :blocked_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:block]) }
scope :bounced_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:bounced]) }
scope :spam_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:spam]) }
scope :invalid_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:invalid]) }
scope :opt_in_users, -> { where(is_subscribed: false, sidekiq_status: STATUS[:opt_in]) }

def self.add_users_from_worksheet(worksheet, column = 1)
fails = []
Expand Down
39 changes: 28 additions & 11 deletions app/views/light/users/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
= render 'light/newsletters/top_navbar'

= simple_form_for (@user), html: {role: 'form', class: 'form-horizontal'} do |f|
%table
%tr
%td
= f.input :email_id, input_html:{class: 'span3'}
= f.input :username, input_html:{class: 'span3'}
= f.input :is_subscribed, :as => :select, input_html:{class: 'span3'}
= f.input :joined_on, input_html: { 'data-behaviour' => 'datepicker'}, input_html:{class: 'span3'}
= f.input :source, input_html:{class: 'span3'}
= f.button :submit, class: "btn btn-success controls"
%center
= simple_form_for (@user), html: {role: 'form', class: 'form-horizontal'} do |f|
%h2 Edit User
%br
%table
%tr
%td
= f.input :email_id, input_html: {class: 'span3'}
= f.input :username, input_html: {class: 'span3'}
= f.input :sent_on, label: 'Last Sent On', input_html: {class: 'span3', readonly: true}, value: @user.sent_on.last
= f.input :source, input_html: {class: 'span3', readonly: true}
= f.input :joined_on, input_html: { 'data-behaviour': 'datepicker'}, input_html:{class: 'span3', readonly: true}
= f.input :subscribed_at, input_html: {class: 'span3', readonly: true}
= f.input :unsubscribed_at, input_html: {class: 'span3', readonly: true}
= f.input :opt_in_mail_sent_at, input_html: {class: 'span3', readonly: true}
= f.input :sidekiq_status, as: :select, collection: Light::User::STATUS.values, label: 'Status', prompt: 'Select Status', selected: @user.sidekiq_status
%br
= f.label 'Blocked'
.make-switch{tabindex: 0, 'data-on': 'success', 'data-off': 'warning', 'data-on-label': 'Yes', 'data-off-label': 'No'}
= f.check_box :is_blocked, {}, 'true', 'false'
%br
%br
= f.label 'Subscribed'
.make-switch{tabindex: 0, 'data-on': 'success', 'data-off': 'warning', 'data-on-label': 'Yes', 'data-off-label': 'No'}
= f.check_box :is_subscribed, {}, 'true', 'false'
%br
%br
= f.button :submit, class: 'btn btn-success controls'

:javascript
$('[data-behaviour~=datepicker]').datepicker({
Expand Down
4 changes: 2 additions & 2 deletions app/views/light/users/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
%td.text-center= '{{email_id}}'
%td.text-center= '{{username}}'
%td.text-center
%a{ :href => "/newsletter/users/{{_id.$oid}}/edit", :class => 'btn btn-sm btn-success'} Edit
%a{ :href => "/newsletter/users/{{id}}/edit", :class => 'btn btn-sm btn-success'} Edit

%a{ :href => "/newsletter/users/{{_id.$oid}}", data:{ :confirm => 'Are you sure?', method: 'delete'}, :class => 'btn btn-sm btn-danger' } Delete
%a{ :href => "/newsletter/users/{{id}}", data:{ :confirm => 'Are you sure?', method: 'delete'}, :class => 'btn btn-sm btn-danger' } Delete

.col-lg-12.padding_none.pull-right
#user_pagination.col-lg-12
Expand Down
9 changes: 7 additions & 2 deletions app/workers/light/import_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ def perform(rows, email_id, source = "Business Card")
rows.each do |row|
email = "#{row[1]}"
name = "#{row[0] || row[1]}"
user = Light::User.create(username: name, email_id: email, source: source,
is_subscribed: false, sidekiq_status: Light::User::NEW_USER) if email.present? or name.present?
user = Light::User.create(
username: name,
email_id: email,
source: source,
is_subscribed: false,
sidekiq_status: Light::User::STATUS[:new_user]
) if email.present? or name.present?
csv << [email, row[0], user.errors.messages] if user.present? and user.errors.present?
end
UserMailer.import_contacts_update(email_id, file_path).deliver
Expand Down

0 comments on commit aafa06f

Please sign in to comment.