I was wondering if it is possible to create a method partial in ruby on rails, for example I have this code;-
@cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
@slots = @cart.slots.first
@slot_list = [@slots.slot_one, @slots.slot_two, @slots.slot_three, @slots.slot_four, @slots.slot_five,
@slots.slot_six, @slots.slot_seven, @slots.slot_eight, @slots.slot_nine, @slots.slot_ten]
@user_products = []
@product = []
@slot_list.each do |item|
if item.nil?
p 'Item empty'
else
@product << item
end
end
@product.each do |item|
items = Product.where(:product_id => item).first
@user_products << items
end
Written in multiple methods to get the @user_products, I was wondering if there was a way so I don't have to write this all the time and possibly run a method or use a partial?
Would it be worth creating a helper that does this and returns the @user_products variable?
I took my own advice and created two helpers, one to return the @user_products and another to return the @total.
I added the names of the methods to our helper_method
helper_method :user_is_admin?, :authenticate_admin!, :product_available?, :get_user_products!, :get_user_total!
then added these two methods at the bottom of the file;-
get_user_products!
def get_user_products!
@cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
@slots = @cart.slots.first
@slot_list = [@slots.slot_one, @slots.slot_two, @slots.slot_three, @slots.slot_four, @slots.slot_five,
@slots.slot_six, @slots.slot_seven, @slots.slot_eight, @slots.slot_nine, @slots.slot_ten]
@user_products = []
@product = []
@slot_list.each do |item|
if item.nil?
p 'Item empty'
else
@product << item
end
end
@product.each do |item|
items = Product.where(:product_id => item).first
@user_products << items
end
return @user_products
end
get_user_total!
def get_user_total!
@total = 0
@cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
@slots = @cart.slots.first
@slot_list = [@slots.slot_one, @slots.slot_two, @slots.slot_three, @slots.slot_four, @slots.slot_five,
@slots.slot_six, @slots.slot_seven, @slots.slot_eight, @slots.slot_nine, @slots.slot_ten]
@user_products = []
@product = []
@slot_list.each do |item|
if item.nil?
p 'Item empty'
else
@product << item
end
end
@product.each do |item|
items = Product.where(:product_id => item).first
@user_products << items
end
@user_products.each do |p|
@total += p.product_price
end
return @total
end
To use these methods inside whatever controller you then do the following;-
@user_products = get_user_products!
@total = get_user_total!
I assume this is in a controller?
What you want is to use plain old Ruby objects (POROs). So, you might have something like this:
class UserProducts
class << self
def get(options={})
@cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
@slots = @cart.slots.first
@slot_list = [
@slots.slot_one,
@slots.slot_two,
@slots.slot_three,
@slots.slot_four,
@slots.slot_five,
@slots.slot_six,
@slots.slot_seven,
@slots.slot_eight,
@slots.slot_nine,
@slots.slot_ten
]
@user_products = []
@product = []
@slot_list.each do |item|
if item.nil?
p 'Item empty'
else
@product << item
end
end
@product.each do |item|
items = Product.where(:product_id => item).first
@user_products << items
end
end
end
Then, in your controller, you'd do something like:
class FooController < ApplicationController
def index
UserProducts.get(user_id: current_user.id)
end
end
So, UserProducts
is essentially a service object. I think some people call them use cases. I tend to call them 'managers'. I put them in their own directory as app/managers/user_products.rb
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.