Saving to DB from Model.rb - ruby-on-rails

When an new order_preview is created, I call USPS for shipping options. If a user updates their zip, I would like the ship_option to reset
Edit: I am no longer calling the intial API call from the view, rather I do an after_create method in the controller.
def get_ship_options
ship_options = {}
#order_preview.fedex_rates.each do |k, v|
if k.service_name == "FedEx Ground Home Delivery" || k.service_name == "FedEx 2 Day" || k.service_name == "FedEx Standard Overnight"
ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
end
end
#order_preview.usps_rates.each do |k, v|
if k.service_name == "USPS Priority Mail 1-Day"
ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
end
end
#order_preview.ship_option_hash = ship_options.map { |k,v| ["#{k} - #{v}","#{k} - #{v}" ] }
#order_preview.save
end
I tried using the answers you guys provided, but the before_save didn't actually save the shiphash the way #order_preview.save does at the end of the above method.
I tried using the same idea, but zip_changed? doesn't work in the controller.
How can I save the new hash that is pulled from the model directly over to the #order_preview ?
From the model I now have
Model.rb
def clear_hash
if zip_changed?
get_shipping_rates
end
end
and
ship_options = {}
fedex_rates.each do |k, v|
if k.service_name == "FedEx Ground Home Delivery" || k.service_name == "FedEx 2 Day" || k.service_name == "FedEx Standard Overnight"
ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
end
end
usps_rates.each do |k, v|
if k.service_name == "USPS Priority Mail 1-Day"
ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
end
end
ship_option_hash = ship_options.map { |k,v| ["#{k} - #{v}","#{k} - #{v}" ] }
**save ship_option_hash to #order_preview.ship_option_hash**

class OrderPreview
before_save :check_state
def check_state
if zip_changed?
ship_option_hash = nil
end
end
...
end
class OrderPreviewController
def update
#order_preview.update(order_preview_params)
end
...
end

Try changing your callback from after_save to before_save. Record considered changed until the changes are not persisted. Changes are lost when you save your object, that's why your record is unchanged when you check for changes in after_save callback.
It should work this way:
before_save :clear_hash, if: :zip_changed?
def clear_hash
ship_option_hash = nil
end
This way the changes will be saved, because you use before_save. In your code, changes were not saved, because you used after_save callback
You controller:
def update
respond_to do |format|
if #order_preview.update(order_preview_params)
flash[:notice] = "Record was successfully updated"
else
flash[:alert] = "Record was not updated"
end
end
end

Related

Ruby on Rails. Large condition in controller

I'm faced a situation unfamiliar to me and I need advice.
There is a controller with the action 'update'
def update
#arrival = find_arrival
#details = #arrival.arrival_details
if check_conditions(#arrival)
flash[:notice] = 'Документ прихода отредактирован'
else
flash[:error] = 'Возникла ошибка. Проверьте правильность заполнения формы'
end
redirect_to edit_admin_arrival_path(#arrival)
end
and few private methods:
def check_conditions(arrival)
new_status = arrival_params[:status]
case #arrival.status
when 'draft'
return unless check_dependencies
recalculate_balance if new_status == 'accrued'
#arrival.update(arrival_params)
when 'canceled'
return unless new_status == 'draft'
#arrival.update(status: arrival_params[:status])
when 'accrued'
return if new_status == 'draft'
recalculate_balance if new_status == 'canceled'
#arrival.update(new_status)
end
end
def recalculate_balance
puts '[PRY] recalculated'
end
def check_dependencies
Provider.exists?(arrival_params[:provider_id]) &&
Warehouse.exists?(arrival_params[:warehouse_id])
end
I'm interested in the following - do I need to move this condition to a separate class or some Service Object for example? I do not think that this huge condition should be in the controller. What can you advise?
Definitely, it's not a Controller logic. Slim controllers are preferred. Better move this one to a service object, or to Arrival class and use as an arrival instance object method #arrival.check_conditions.
And I'd recommend to use state machine here: https://github.com/pluginaweek/state_machine

Ruby how to modify parameters

so i have this code that and my aim was to convert any empty string to null
def convert_empty_strings_to_null
if request.patch? || request.post?
convert_empty_strings_to_null_rec(request.params)
end
end
def convert_empty_strings_to_null_rec(param)
param = nil if param.empty? if param.is_a?(String)
param.all?{|v| convert_empty_strings_to_null_rec v} if param.is_a?(Array)
param.all?{|k,v| convert_empty_strings_to_null_rec v} if param.is_a?(Hash)
end
But i'm new to ruby on rails and i found it that it sends params by value and not by reference, so no change in params is made, how do i fix this ?
I assume that by "empty" you mean zero-with strings, meaning that strings consisting only of whitespace should be left intact. (Otherwise blank? and strip would be your friends.)
def convert_empty_strings_to_nil
if request.patch? || request.post?
request.params.each do |key, value|
request.params[key] = convert_empty_strings_to_nil_rec(value)
end
end
end
def convert_empty_strings_to_nil_rec(param)
case param
when String
param.empty? ? nil : param
when Array
param.map{ |v| convert_empty_strings_to_nil_rec(v) }
when Hash
param.map{ |k,v| [k, convert_empty_strings_to_nil_rec(v)] }.to_h
else
param
end
end
First of all, this is how your convert_empty_strings_to_null_rec method should be, for keeping the changes persistent:
def convert_empty_strings_to_null_rec(param)
if param == ""
updated_param == nil
elsif param.is_a?(Array)
updated_param == param.map{|x| nil if x.empty? }
elsif param.is_a?(Hash)
updated_param = {}
param.each do |k, v|
if v.empty?
updated_param[k] = nil
else
updated_param[k] = v
end
end
end
return updated_param
end
Further, I am assuming from your question that convert_empty_strings_to_null is a action method. It should be updated to catch what convert_empty_strings_to_null_rec method is returning.
def convert_empty_strings_to_null
if request.patch? || request.post?
updated_params = convert_empty_strings_to_null_rec(request.params)
end
# you can use the updated_params here on in this action method
end
Hope it helps : )

One method for two models. How to pass name of model as variable to controller?

I have two methods in two different controllers (Posts & Boards). They are almost same. The difference is only model-instance-association name. To DRY this I think to write the method in module, but how to share it between Post and Board?
def init_post_comments
#user = current_user
a = #user.posts.pluck(:id) # not very nice...
b=params[:post_ids] ||= []
b = b.map(&:to_i)
follow = b - a
unfollow = a - b
follow.each do |id| # checkbox just checked
#post = Post.find_by_id(id)
if #post.users.empty?
#post.update_attribute(:new_follow, true)
end
#user.posts << #post
end
unfollow.each do |id| # if checkbox was unchecked
#post = Post.find_by_id(id)
remove_post_from_user(#post)# here we destroy association
end
if follow.size > 0
get_post_comments_data
end
redirect_to :back
end
UPDATE Ok, if I'll move the methods to model's concern how I should work with associations here? Here #user.posts.pluck(:id) and here #user.boards.pluck(:id) with what I can replace posts and boards so it can work with both of them?
So, I did it! I don't know if it's right way, but I DRY this code.
Two controllers:
posts_controller.rb
def init_comments
if Post.comments_manipulator(current_user, params[:post_ids] ||= []) > 0
#posts = Post.new_post_to_follow
code = []
#posts.each do |post|
group = post.group
code = code_constructor('API.call')
end
Post.comments_init(get_request(code), #posts)
end
redirect_to :back
end
boards_controller.rb
def init_comments
if Board.comments_manipulator(current_user, params[:board_ids] ||= []) > 0
#boards = Board.new_board_to_follow
code = []
#boards.each do |board|# подготовка запроса
group = board.group
code = code_constructor('API.call')
end
Board.comments_init(get_request(code), #boards)
end
redirect_to :back
end
As you can see they are absolutely same.
In models board.rb and post.rb - include CommentsInitializer
And in models\concerns
module CommentsInitializer
extend ActiveSupport::Concern
module ClassMethods
def comments_manipulator(user, ids)
relationship = self.name.downcase + 's'
a = user.send(relationship).pluck(:id)
b = ids.map(&:to_i)
follow = b - a
unfollow = a - b
follow.each do |id| # start to follow newly checked obj
#obj = self.find_by_id(id)
if #obj.users.empty?
#obj.update_attribute(:new_follow, true)
end
user.send(relationship) << #obj
end
unfollow.each do |id| # remove from following
#obj = self.find_by_id(id)
remove_assoc_from_user(#obj, user)#destroy relation with current user
end
follow.size
end
def comments_init(comments, objs)
i = 0
objs.each do |obj| # updating comments data
if comments[i]['count'] == 0
obj.update(new_follow: false)
else
obj.update(new_follow: false, last_comment_id: comments[i]['items'][0]['id'])
end
i += 1
end
end
def remove_assoc_from_user(obj, user)
user = user.id
if user
obj.users.delete(user)
end
end
end
My code works. If you know how to make it better please answer!

How to handle multiple conditions of instance variable assignment

I have the following in my controller that will assign a different collection of results depending on what params are received with an Ajax call. It is messy and i would like to just call a function with all the logic in rather than all this in my index controller
class PublicController < ApplicationController
def index
if params[:literacy_param].present?
#skills = Skill.search(params)
elsif params[:numeracy_param].present?
#skills = Skill.numeracy_default_params
elsif params[:numeracy_number_skills].present?
#skills = Skill.numeracy_number_skills
elsif params[:numeracy_measuring_skills].present?
#skills = Skill.numeracy_measuring_skills
elsif params[:numeracy_data_skills].present?
#skills = Skill.numeracy_data_skills
else
#skills = Skill.default_params
end
end
end
Im just a bit unsure on how to set out my function so that it can read the params that are being sent,
I have come up with this so far
private
def skills(params)
if params[:literacy_param].present?
#skills = Skill.search(params)
elsif params[:numeracy_param].present?
#skills = Skill.numeracy_default_params
elsif params[:numeracy_number_skills].present?
#skills = Skill.numeracy_number_skills
elsif params[:numeracy_measuring_skills].present?
#skills = Skill.numeracy_measuring_skills
elsif params[:numeracy_data_skills].present?
#skills = Skill.numeracy_data_skills
else
#skills = Skill.default_params
end
end
Then in my index action i would do
#skills = skills(params)
would this be an efficient way?
Thanks
You can do this
class PublicController < ApplicationController
def index
skills = ['literacy_param', 'numeracy_param', 'numeracy_number_skills', 'numeracy_measuring_skills', 'numeracy_data_skills']
common_in_params = (skills & params).first
#skills = common_in_params.present? ? (common_in_params.eql?('literacy_param') ? Skill.search(params) : Skill.send(common_in_params)) : Skill.default_params
end
end
You can define skills array in an initializer for resusability
One way of doing it would be this:
def skills(params)
set_of_skills = params.slice(
:numeracy_param,
:numeracy_number_skills,
:numeracy_measuring_skills,
:numeracy_data_skills,
).first
#skills = if params[:literacy_param]
Skill.search(params)
elsif set_of_skills
Skill.public_send(set_of_skills)
else
Skill.default_params
end
end
I would also advise to have this extracted into a lib/ folder, and unit-tested. So that in your controller you could perform the following:
def index
#skills = SkillSearch.new(params).search
end
Two ways I can think of doing this right now:
Wrap the params in a unique key. As in params = { :keyword => :literacy_param }, and then use this unique key to identify the right operation.
In you skill.rb:
def self.filter(params)
if params[:keyword] == :literacy_param
search(params)
elsif available_filters.include?(params[:keyword])
public_send(params[:keyword])
else
default_params
end
end
private
def self.available_filters
%i{numeracy_default_params numeracy_number_skills numeracy_measuring_skills numeracy_data_skills}
end
considering that instead of :numeracy_param, you send :numeracy_default_params in :keyword key. Otherwise you'll have to make another elsif inside filter method.
then in your index method:
def index
#skilles = Skill.filter(params)
end
You create a separate filter class, which is an expandable solution, just in case when you need to go for complex search queries and filtering.
Let's call it SkillSeacrher, inside you app/models/skill_searcher.rb:
class SkillSearcher
attr_reader :keyword
def initialize(keyword)
#keyword = keyword
end
def filter
if keyword == :literacy_param
Skill.search(params)
elsif available_filters.include?(keyword)
Skill.public_send(keyword)
else
Skill.default_params
end
end
private
def self.available_filters
%i{numeracy_default_params numeracy_number_skills numeracy_measuring_skills numeracy_data_skills}
end
end
then in index method:
def index
#skills = SkillSearcher.new(params[:keyword]).filter
end
However, you can do one more change to filter method(depends on your taste):
def filter
if keyword == :literacy_param
Skill.search(params)
else
Skill.public_send(available_filters.include?(keyword) ? keyword : :default_params)
end
end
And, if you have all these methods accepting params as arguments then it'd be much more sleek:
def filter
Skill.public_send(available_filters.include?(keyword) ? keyword : :default_params, params)
end

Trying to perform a method on a params in Rails

I want to write a method that loops through all the params to make sure they aren't all blank.
My params are:
params[:search][:company]
params[:search][:phone]
params[:search][:city]
params[:search][:state]
params[:search][:email]
I have this method:
def all_blank_check(params)
array=[]
params[:search].each do |key, value|
array << value unless value.blank?
end
if array.count < 1
return true
else
return false
end
end
But when I try something like all_blank_check(params) I get the following error:
NoMethodError (undefined method `all_blank_check' for #<Class:0x108c08830>):
Do I need to convert the params to an array first? Can't I perform a method on params?
Edit - full source:
def index
#customers = Customer.search_search(params)
end
def self.search_search(params)
search_field = []
search_values = []
array = []
test = ''
if !params[:search].nil? && all_blank_check(params[:search]
if !params[:search].nil? && !params[:search][:company].blank?
search_field << 'customers.company LIKE ?'
search_values << "%#{params[:search][:company]}%"
end
if !params[:search].nil? && !params[:search][:city].blank?
search_field << 'customers.city = ?'
search_values << "#{params[:search][:city]}"
end
if !params[:search].nil? && !params[:search][:phone].blank?
search_field << 'customers.phone_1 = ?'
search_values << "%#{params[:search][:phone]}%"
end
conditions = [search_field.join(' AND ')] + search_values
Customer.where(conditions).includes(:customer_contacts).limit(10)
end
end
def all_blank_check(params)
params[:search].each do |key, value|
array << value unless value.blank?
end
if array.count < 1
return false
end
if array.count > 1
return true
end
end
You can also use more Ruby-minded code like this:
def self.all_blank?(params)
params[:search].count{|key, value| !value.blank?} == 0
end
This counts the values that are not blank; if the number is 0, it means all are blank.
It avoids creating a new array just for counting.
The problem is not the type of params, the problem is that the method all_blank_check does not exist on the object you call it on.
You defined it as an instance method and you're trying to call it from the class method search_param, which won't work.
If you want to make all_blank_check a class method you need to change the definition to def self.all_blank_check(params) - same as search_param.

Resources