Post has_many :comments
Comment belongs_to :post
I want to bulk update (approve) all comments of posts that are not cancelled. If there is a cancelled comment selected, display the notification else approve comments.
def make_comments_approved
comment_ids = params[:comment_ids]
if comment_ids
posts = Post.includes(:comments).
where(comments: { id: comment_ids.keys })
comments = posts.comments
cancelled_comments = posts.
where(comments: { status: :cancelled }).comments
comments_to_update = comments - cancelled_comments
if cancelled_comments.any?
flash[:error] = "Cannot approve cancelled comments: #{cancelled_comments.pluck(:number)}"
else
approve_comments(comments_to_update)
flash[:notice] =
"#{pluralize(comments.count, 'comment')} successfully approved"
end
else
flash[:error] = 'Select comments to update'
end
redirect_to :back
end
private
def approve_comments(comments)
# approve here
end
Use update_all for bulk update. For example:
comments.update_all(approved: true)
However, be cautious as it does not trigger validations or callbacks.
If you have comment_ids with you do following
cancelled_comment = Comment.where(id: comment_ids.keys, status: :cancelled)
if cancelled_comment.count > 0
flash[:error] = "Cannot approve cancelled comments: #{cancelled_comments.pluck(:number)}"
else
comments = Comment.where(id: comment_ids.keys).update_all()
flash[:notice] = "#{pluralize(comments.count, 'comment')} successfully approved"
end
If you are using the search-logic gem, you can do the following (Syntax is of Rails 2)
Comment.status_ne("cancelled").update_all(:approved => true)
Related
I have an invite method that sends users emails to be part of a team. I have a modal that displays some users that already have an account and a textfield to input more emails and sends an invite for the person to join the platform.
But if I type an email that already exists in the database I get:
Validation failed: Email has already been taken
But want to send an email even if the person already has an account.
Here's what I have:
def invite
invite = params.dig(:invite) || {}
candidate_ids = invite.dig(:candidate_ids) || []
extra_emails = invite.dig(:extra_emails) || ""
emails = extra_emails
.split(/,|;/)
.map(&:strip)
new_users = emails.map { |email| team_email_invite(email) }
candidate_ids.concat new_users.map(&:id)
invite_data = candidate_ids.map { |uid| {
:user_id => uid,
:team_id => #team.id,
} }
TeamInvite.create(invite_data)
.map!(&:email)
respond_to do |format|
format.html { redirect_to overviews_url, notice: 'Invites sent.' }
format.json { head :no_content }
end
end
def team_email_invite(email)
user = User.new({
:email => email,
:password => SecureRandom.hex,
:role => :shadow,
})
user.add_role :recruiter
user.skip_confirmation_notification!
user.save!
end
candidate_ids - It's the users that I display on the list(all good)
extra_emails - emails in the textfield
So when I write a user that already has an account in the textfield, team_email_invite tries to create a new user and crashes.
I don't want to do something like
begin
new_users = emails.map { |email| team_email_invite(email) }
rescue
new_users=[]
end
because then it doesn't send the invite.
Any idea how to solve this?
You could use first_or_initialize. The block only gets run if the User does not already exist. Here's an example...
user = User.where(email: email).first_or_initialize do |usr|
usr.email = email
usr.password = SecureRandom.hex
usr.role = :shadow
usr.skip_confirmation_notification!
end
user.add_role :recruiter
user.save!
everyone.
I'm gonna test an active record object destroy failure but I've problems creating a failure situation.
I have a before_filter method called 'require_user_payment_info' which validates the #payment_info object before the delete method is called, so I can't create a 'bad' #payment_info object before the delete method is called.
Here's the require_user_payment_info method:
def require_user_payment_info
#payment_info = credit_card_model.slave.find_by_user_guid(user_guid)
if !#payment_info || #payment_info.user_guid != user_guid
redirect_to(:controller => 'store', :action => 'index') and return false
else
if((#payment_info.card_expires_year.to_i < Date.today.year) ||
((#payment_info.card_expires_month.to_i < Date.today.month) && (#payment_info.card_expires_year.to_i == Date.today.year)))
#payment_info.card_account_public = "" #clear this out so the user is forced to re-enter the credit card number
#payment_info.valid?
flash.now[:error] = t('ui_flash_errors.card_expired_error')
end
end
end
And the actual delete method:
def delete
# required to be called via a delete request
redirect_to :action => 'edit' and return if !request.delete?
if #payment_info.destroy
flash[:notice] = "Delete SUCCESSFUL"
redirect_to :action => 'new'
else
flash[:error] = "Delete failed"
redirect_to :action => 'edit'
end
Any ideas?
This is my solution:
def test_delete
payment_info = Factory.create(:payment_info, :user_guid=>#user.guid, :card_expires_month=>'04',
:card_expires_year=>(Date.today.year+2).to_s, :cardholder_city=>"test city",
:cardholder_state=>'NC', :cardholder_country=>'US', :cardholder_zip=>'27612')
PaymentInfo.any_instance.stubs(:destroy).returns(false)
delete(:delete, {}, #session)
assert_response(:redirect)
assert_equal false, assigns(:payment_info).blank?
assert_redirected_to({:controller=>'account', :action=>'edit'})
assert_equal flash[:error], "There was an error deleting your credit card information. Please try again."
end
I am new to ruby/rails/spree. I am implementing Indian payment gateway with spree-3.0.7.
I am able to process the order but payment status is always at balance_due.
Controller code
def confirm
payment_method = Spree::PaymentMethod.find(payment_method_id)
Spree::LogEntry.create({
source: payment_method,
details: params.to_yaml
})
order = current_order || raise(ActiveRecord::RecordNotFound)
if(address = order.bill_address || order.ship_address)
firstname = address.firstname
end
#confirm for correct hash and order amount requested before marking an payment as 'complete'
checksum_matched = payment_method.checksum_ok?([params[:status], '', '', '', '', '', '', params[:udf4], params[:udf3], params[:udf2], params[:udf1], order.email, firstname, #productinfo, params[:amount], params[:txnid]], params[:hash])
if !checksum_matched
flash.alert = 'Malicious transaction detected.'
redirect_to checkout_state_path(order.state)
return
end
#check for order amount
if !payment_method.amount_ok?(order.total, params[:amount])
flash.alert = 'Malicious transaction detected. Order amount not matched.'
redirect_to checkout_state_path(order.state)
return
end
payment = order.payments.create!({
source_type: 'Spree::Gateway::Payumoney',#could be something generated by system
amount: order.total,
payment_method: payment_method
})
payment.started_processing!
payment.pend!
order.next
order.update_attributes({:state => "complete", :completed_at => Time.now})
if order.complete?
order.update!
flash.notice = Spree.t(:order_processed_successfully)
redirect_to order_path(order)
return
else
redirect_to checkout_state_path(order.state)
return
end
end
Gateway/Model Code
require "offsite_payments"
module Spree
class Gateway::Payumoney < Gateway
preference :merchant_id, :string
preference :secret_key, :string
def provider_class
::OffsitePayments.integration('Payu_In')
end
def provider
#assign payment mode
OffsitePayments.mode = preferred_test_mode == true ? :test : :production
provider_class
end
def checksum(items)
provider_class.checksum(preferred_merchant_id, preferred_secret_key, items)
end
def auto_capture?
true
end
def method_type
"payumoney"
end
def support?(source)
true
end
def authorization
self
end
def purchase(amount, source, gateway_options={})
ActiveMerchant::Billing::Response.new(true, "payumoney success")
end
def success?
true
end
def txnid(order)
order.id.to_s + order.number.to_s
end
def service_provider
"payu_paisa"
end
def checksum_ok?(itms, pg_hash)
Digest::SHA512.hexdigest([preferred_secret_key, *itms, preferred_merchant_id].join("|")) == pg_hash
end
def amount_ok?(order_total, pg_amount)
BigDecimal.new(pg_amount) == order_total
end
end
in spree payment doc https://guides.spreecommerce.com/developer/payments.html they have mentioned if auto_capture? return true then purchase method will be called but purchase method is not getting called.
Can anyone point me to right direction?
You need not call the following commands
payment.started_processing!
payment.pend!
Just leave the payment in its initial state. i.e. checkout state and complete your order.
Because when order is completed process_payments! is called.
This method processes unprocessed payments whose criteria is like below
def unprocessed_payments
payments.select { |payment| payment.checkout? }
end
Hope this solves your case :)
I fixed the issue by marking payment as complete.
remove
payment.started_processing!
payment.pend!
add
payment.complete
above
order.next
I have published my code at github as gem
https://github.com/isantoshsingh/spree_payumoney
I have three models: Employee, User, and Role. Relationship between these classes are employee --- one_to_one -- user and user -- one_to_many -- role.
My create action is working fine with following strong params methods
def employee_params
params.require(:employee).permit(:first_name, :middle_name, :last_name, :email, user_attributes: [:id, role_ids:[]])
end
For update if employee record has no user object, I am instantiating new user, mapping it to employee and calling update by passing string params. However, the update is failing with message:
Failed to save the new associated user
My update method code is
def update
#employee = Employee.find(params[:id])
if params[:employee][:user_attributes] != nil && params[:employee][:user_attributes][:role_ids] != nil && ! params[:employee][:user_attributes][:role_ids].empty?
if #employee.user == nil
#employee.user = User.new
temp_password = Devise.friendly_token.first(8)
#employee.user.is_temp_password = true
#employee.user.password = Devise.friendly_token.first(8)
#employee.user.email = #employee.email
#employee.user.email = params[:employee][:email] if params[:employee][:email]
end
end
respond_to do |format|
if #employee.update(employee_params)
format.json { render json: #employee.as_json}
else
format.json {render :json => #employee.errors.as_json, :status => 422}
end
end
end
I think as the above users suggested, you need to save the new User Object but also I think you should have the User creation code inside of the Employee create since you you would need to auto create it anyways in the update
Not sure if you also aware of helpers blank?, present? but I rewrote your code with that
def update
#employee = Employee.find(params[:id])
if params[:employee][:user_attributes].present? && params[:employee][:user_attributes][:role_ids].present? && params[:employee][:user_attributes][:role_ids].present?
unless #employee.user
user = User.new
#employee.user = user
temp_password = Devise.friendly_token.first(8)
user.is_temp_password = true
user.password = Devise.friendly_token.first(8)
user.email = #employee.email
user.email = params[:employee][:email] if params[:employee][:email]
user.save!
end
end
I want users to be able to redeem their accounts, after they purchase them through a redemption code.
class Code < ActiveRecord::Base
#redemption_codes = Account.where(:redeemed == false).map(&:redemption_code)
validates :code, inclusion: { in: #redemption_codes }
end
Here's my create method in my AccountsController
def create
#code = Code.new(code_params)
#account = Account.where(redemption_code: #code)
if #code.save
#account.redeemed == true
flash[:notice] = "Account Redeemed"
redirect_to root_path
else
flash[:danger] = "This does not match any accounts"
render 'new'
end
end
After a code is saved, a user entering a redemption code, I want the :redeemed value of the item to change from false to true.
I got an error, I have yet to see before
Undefined Method 'redeemed' for #<ActiveRecord::Relation[]>
Not sure why though
It's because when you call Account.where(redemption_code: #code), that may return multiple accounts. You need to do something like Account.where(redemption_code: #code).first or Account.find_by_redemption_code(#code).