Remove white space from params - ruby-on-rails

I have a piece of code which collects params from a form and reads them into invite as I can see. The problem is one of these parameters is an email which I want to ensure has no trailing whitespace. How could I apply strip to invite and all of the params fed into it?
def invite_partner
return unless invite = params[:partner]
return flash.now[:error] = 'Please select a role' unless role = Role.where(klass: invite[:klass], name: 'Admin').first
return flash.now[:alert] = 'Email address already in use' if User.where(email: invite[:email]).first
raise 'bugger' unless current_user.is_a?(User)
partner_invite = PartnerInviteMailer.invite(current_user.operator, invite[:email], invite[:klass], Portal.portal_for_hostname(host_from_request))
partner_invite.deliver
flash.now[:success] = "#{invite[:klass]} invited, email sent to #{invite[:email]}"
end

Does this help?
params[:partner].each { |key, value| value.strip! }

Skip array's like related ids.
params[:partner].each { |key, value| value.strip! unless value.kind_of?(Array) }

You can use the try method, which will handle Arrays, nil values, etc without having to check kind_of?.
params[:partner].each { |key, value| value.try(:strip!) }

Related

updating the database after create action doesn't work

set_bonus(member_id, cookie) method does not work. I'm trying to update the same model that that the self.set_signup_attribution(member_id, cookie, origin) returns.
The new_has_value variable returns {"currency"=>"usd", "type"=>"flat", "amount"=>1000}
Model.rb
# THIS METHOD WORKS
def self.set_signup_attribution(member_id, cookie, origin)
return unless cookie.present?
tracking_code = cookie
attribution_channel = AttributionChannel.find_by tracking_code: tracking_code
associated_member_record = Member.find member_id
if attribution_channel.present?
Attribution.create!({
event: Attribution::SIGN_UP,
attribution_channel: attribution_channel,
associated: associated_member_record,
extra: origin
})
set_bonus(member_id, cookie)
else
Rails.logger.info "Unknown Attribution Channel for tracking code: '#{ tracking_code }'"
end
end
# THIS METHOD DOES NOT WORK. UPDATES THE DATABASE.
def self.set_bonus(member_id, cookie)
epoch = Member.find_by(id: member_id).attribution_epoch
attribution_code = AttributionChannel.find_by(tracking_code: cookie)
duration_value = attribution_code.attribution_duration.downcase.split(' ')
duration = duration_value.first.to_i.send(duration_value.last)
return if cookie.present? && epoch.present?
current_time = Time.now
if attribution_code.bonus_config.present?
if (current_time - epoch).to_i < duration
hash_value = attribution_code.bonus_config
new_hash_value = hash_value.assoc("sign_up")[1]
value = Attribution.where(attribution_channel_id: attribution_code)
if new_hash_value["type"] == "flat"
value.update_all(
bonus_amount: new_hash_value["amount"],
bonus_currency: new_hash_value["currency"]
)
elsif new_hash_value["type"] == "percentage"
value.update_all(
bonus_amount: new_hash_value["amount"],
bonus_currency: new_hash_value["currency"]
)
else
{
bonus_amount: "Doesn't exist",
bonus_currency: "Doesn't exist"
}
end
else
"Do nothing"
end
else
"Do nothing"
end
#cookie = nil
binding.pry
end
Controller.rb
def index
unless session[:just_signed_up]
redirect_back_or_settings_page
end
Attribution.set_signup_attribution(current_user, cookies[:visit_attr], request.referer)
Attribution.set_bonus(current_user, cookies[:visit_attr])
session[:just_signed_up] = false
#email = current_user.email
end
How do I go about this? That is what I have tried and doesn't work. Can I merge set_bonus method to set_signup_attribution method or something?
Any help will be appreciated.
So drilling this further:
I merged set_bonus with set_signup_attribution and the two fields (bonus_amount and bonus_currency) which set_bonus method is supposed to update returns nil:
Attribution.create!(
{
event: Attribution::SIGN_UP,
attribution_channel: attribution_channel,
associated: associated_member_record,
extra: origin
}.merge(self.set_bonus(member_id, cookie).to_h)
)
With this drill after using binding.pry on that set_bonus method, I figured out it worked but it's returning nil and I don't know why. Could it be because member_id is not available in the model or something?
in your if statement you should call set_bonus method on appropriate object.
attribution = Attribution.create!({
event: Attribution::SIGN_UP,
attribution_channel: attribution_channel,
associated: associated_member_record,
extra: origin
})
attribution.set_bonus(member_id, cookie) if attribution.persisted?
Just be careful as .create! will raise an error in case there is something wrong, so maybe would be better to use
attribution = Attribution.new(.....)
if attribution.save
attribution.set_bonus(.....)
else
Rails.logger.info attribution.errors
end
I hope this would help.
Cheers

How to append items to an array

I have the following:
myObject = {
id: user.id,
email: user.email,
}
I need to add values like so:
if current_user && current_user.id == user.id
myObject << {
notification_email: user.notification_email,
notification_email2: user.notification_email2
}
end
The code above raises an error.
What's the right way to optionally append values to the object?
ERROR
undefined method `<<' for # Did you mean? <
Perhaps you want Hash#merge.
myObject.merge(
notification_email: user.notification_email,
notification_email2: user.notification_email2
)
If you want side effects, use the banged version.
myObject.merge!(
notification_email: user.notification_email,
notification_email2: user.notification_email2
)
myObject is a hash, so to add new items you can do this:
if current_user && current_user.id == user.id
myObject[:notification_email] = user.notification_email
myObject[:notification_email2] = user.notification_email2
end

How to test a specific line in a rails model using rspec

I have a model with an initializer in it, which basically creates a user from a user hash.
After it gets the user information, it checks whether the "privileges" key in the hash is an array. If it's not, it turns it into an array.
Now the obvious way of doing this would be crafting an entire user_hash so that it would skip those "create user" lines and then check if it turns the input into an array if necessary. However, I was wondering if there is a more DRY way of doing this?
Here is the user model I'm talking about:
def initialize(opts={})
#first_name = opts[:user_hash][:first]
#last_name = opts[:user_hash][:last]
#user_name = opts[:user_hash][:user_name]
#email = opts[:user_hash][:email]
#user_id = opts[:user_hash][:id]
#privileges = {}
if opts[:privs].present?
if !opts[:privs].kind_of?(Array)
opts[:privs] = [opts[:privs]]
end
end
end
You can pass a double which returns the needed value when the proper key is requested, and itself (or something else) otherwise:
it 'turns privs into an array' do
opts = double(:opts)
allow(opts)to receive(:[]).and_return(opts)
allow(opts)to receive(:[]).with(:privs).and_return('not array')
expect(MyClass.new(opts).privileges).to eq(['not array'])
end
Btw, your code could be simplified using the splat operator:
privs = [*opts[:privs]]
sample behavior:
privs = nil
[*privs]
# => []
privs = ['my', 'array']
[*privs]
# => ["my", "array"]
privs = 'my array'
[*privs]
# => ["my array"]
You can even use the idempotent Kernel#Array
def initialize(opts = {})
#first_name = opts[:user_hash][:first]
#last_name = opts[:user_hash][:last]
#user_name = opts[:user_hash][:user_name]
#email = opts[:user_hash][:email]
#user_id = opts[:user_hash][:id]
#privileges = {}
Array(opts[:privs])
end
I hope that helps
Rather than testing the implementation (value is turned into an array), I would test the desired behavior (takes single privilege or multiple privileges):
describe User do
describe '#initialize' do
it "takes single privilege" do
user = User.new(user_hash: {}, privs: 'foo')
expect(user.privileges).to eq(['foo'])
end
it "takes multiple privileges" do
user = User.new(user_hash: {}, privs: ['foo', 'bar'])
expect(user.privileges).to eq(['foo', 'bar'])
end
end
end

inject method to retrieve data from hash

I'm having trouble getting the method below in my user model to handle a hash ('auth') I'm getting from LinkedIn for user signin:
def self.deep_get auth, *fields
auth.inject(auth) { |acc, e| acc[e] if acc }
end
I call the 'deep_get' method later in my user model as I create a user using omniauth/linkedin gem. However, it's returning nil values for the provider/uid/headline/email user fields that I know are not nil.
I included first_name and last_name fields as an example because this approach is working (not returning nil values), but (as I realize) bad style/exception handling. Any ideas as to why my deep_get inject method isn't working to retrieve the data in the hash as I'd like it to?
def self.create_from_omniauth(auth)
create! do |user|
# i'd like to retrieve user information from linkedin per the following with my inject method, but i am getting nil values when i should be getting data.
# :provider and :uid are on the same branch level of data. first_name,last_name,email,etc. are on a branch just below called 'info'
user.provider = deep_get(auth, :provider)
user.uid = deep_get(auth, :uid)
user.headline = deep_get(auth, :info, :headline)
user.email = deep_get(auth, :info, :email)
# the below is working but i know pokemon exception handling is not good style.
begin
user.first_name = auth["info"]["first_name"]
rescue
end
begin
user.last_name = auth["info"]["last_name"]
rescue
end
try this
def deep_find(obj,key)
if obj.respond_to?(:key?) && obj.key?(key)
obj[key]
elsif obj.respond_to?(:each)
r = nil
obj.find{ |*a| r=deep_find(a.last,key) }
r
end
end
or try this
class Hash
def deep_fetch(key, default = nil)
default = yield if block_given?
(deep_find(key) or default) or nil
end
def deep_find(key)
if key?(key)
self[key]
else
self.values.inject(nil) do |memo, v|
memo = v.deep_find(key) if v.respond_to?(:deep_find)
memo unless memo.nil?
end
end
end
end

Using Haml Helpers That Return Strings

I love using Haml helpers, but over the years things have changed a bit. The old way was simply to concatenate to the buffer. Here's what I have:
def confirmation_table(field)
# Be certain that if the user is logged in, his/her email and name show
if field.respond_to? :user
haml_tag('tr') {
haml_tag('th', 'Email:')
haml_tag('td', field.user.email)
}
haml_tag('tr') {
haml_tag('th', 'Name:')
haml_tag('td', field.user.full_name)
}
else
haml_tag('tr') {
haml_tag('th', 'User Information:')
haml_tag('td', 'Not specified.')
}
end
field.class.columns.collect{|col| col.name}.reject{|col|
col =~ /_at$/ ||
col =~ /_on$/ ||
col =~ /_id$/ ||
col == 'id'}.each do |col|
haml_tag('tr') {
haml_tag('th', ActiveSupport::Inflector::humanize(col))
haml_tag('td', typeize(field, col))
}
end
end
This can, of course, be accessed in my view as simply as:
- confirmation_table(#f)
However, it makes more sense (to me) for this to return a string. I can't see how haml_capture provides the same structuring ability. Any hints?
Wrap your haml_tag calls in capture_haml:
def confirmation_table(field)
capture_haml do
if field.respond_to? :user
haml_tag(:tr) do
haml_tag('th.email', 'Email:')
haml_tag('td.email', field.user.email)
end
# ...
end
end
end
They'll be captured and returned by capture_haml.

Resources