RAILS: db count is always 0 in spec - ruby-on-rails

Here is a simple case of spec in Rails 4.2:
it "returns redirect to Save & Copy" do
user_access = FactoryGirl.create(:user_access, :action => 'create', :resource =>'simple_orderx_orders', :role_definition_id => #role.id, :rank => 1,
:sql_code => "")
session[:user_id] = #u.id
q1 = FactoryGirl.create(:simple_orderx_order)
q = FactoryGirl.attributes_for(:simple_orderx_order)
FactoryGirl.create(:project_misc_definitionx_misc_definition, :resource_id => q1.id, :resource_string => 'simple_orderx_orders', definition_category: 'production_step')
expect {
get 'create', {:order => q, commit: I18n.t('Save & Copy')}
expect(response).to redirect_to URI.escape(SUBURI + "/view_handler?index=0&msg=Successfully Saved!")
}.to change(ProjectMiscDefinitionx::MiscDefinition.all.reload, :count).by(1)
end
end
In debugger, there is a new record being added to table MiscDefinition and MiscDefinition.all.count is 2, but in spec the ProjectMiscDefinitinox::MiscDefinition.all.count is always 0 and the spec case fails. What could cause the counting error in spec?
Here is the create in controller:
def create
#order = SimpleOrderx::Order.new(new_params)
#order.last_updated_by_id = session[:user_id]
#order.entered_by_id = session[:user_id]
#order.fort_token = session[:fort_token]
copy_steps(#order) if params[:commit] == I18n.t('Save & Copy') #copy mfg steps from previous order
if #order.save
copy_steps(#order.reload) if params[:commit] == I18n.t('Save & Copy') #copy mfg steps from previous order
redirect_to URI.escape(SUBURI + "/view_handler?index=0&msg=Successfully Saved!")
else
#erb_code = find_config_const('order_new_view', session[:fort_token], 'simple_orderx')
flash[:notice] = t('Data Error. Not Saved!')
render 'new'
end
end
def copy_steps(order)
obj = Order.where('drawing_num = ? AND id != ?', order.drawing_num, order.id).order('created_at DESC') if order.drawing_num.present? #find the previous order of the same drawing#
obj = Order.where('part_num = ? AND id != ?', order.part_num, order.id).order('created_at DESC') if obj.blank? && order.part_num.present?
obj = Order.where('part_name Like ? AND id != ?', "%#{order.part_name}%", order.id).order('created_at DESC') if obj.blank? && order.part_name.present?
copied = false
obj.limit(4).each do |o|
SimpleOrderx.production_step_class.where('resource_id = ? AND resource_string = ? AND definition_category = ?', o.id, params[:controller].sub('/', '_'), 'production_step').each do |r|
new_step = SimpleOrderx.production_step_class.new()
new_step = r.dup
new_step.resource_id = order.id
begin
new_step.save
copied = true
rescue => e
flash[:notice] = 'Error in copying production steps: ' + e.message
end
end
return if copied
end if obj.present?
end

Related

I'm trying to use the rufus scheduling gem from the controller to pull the lowest price from an api every min

this is in my initializer which works perfectly for pulling all lowest prices and avg price every 5, 10, 20, etc. seconds
unless defined?(Rails::Console) || File.split($0).last == 'rake'
s = Rufus::Scheduler.singleton
s.every '10m' do
Rails.logger.info "hello, it's {Time.now}"
Rails.logger.flush
Bid.all.each do |bid|
id = bid.event_id
#events = Unirest.get("https://api.seatgeek.com/2/events/#{id}?&client_id=NjQwNTEzMXwxNDgxNDkxODI1").body
if #events["stats"]
#low = #events["stats"]["lowest_price"] || 0
#avg = #events["stats"]["average_price"] || 0
BuyNowBid.create(bid_id: bid.id, lowest_price: #low , average_price: #avg)
end
end
end
end
But when i try to create a bid from the controller and then try pull the lowest and average price from the api it doesn't work been sitting been working on this for a few hours now any tips would be much appreciated.
def create
##bid?
#seatgeek id
#bidd = Bid.new(event_id: params[:event_id], user_id: session[:user_id], bid: params[:bid], lowest_price: params[:lowest_price])
if session[:user_id] == current_user.id
#bidd.save
#events = Unirest.get("https://api.seatgeek.com/2/events/#{#bidd.event_id}?&client_id=NjQwNTEzMXwxNDgxNDkxODI1").body
if #events["stats"]
#low = #events["stats"]["lowest_price"] || 0
#avg = #events["stats"]["average_price"] || 0
BuyNowBid.create(bid_id: #bidd.id, lowest_price: #low , average_price: #avg)
end
Rufus::Scheduler.singleton.every '10s' do
#events = Unirest.get("https://api.seatgeek.com/2/events/#{#bidd.event_id}?&client_id=NjQwNTEzMXwxNDgxNDkxODI1").body
if #events["stats"]
#low = #events["stats"]["lowest_price"] || 0
#avg = #events["stats"]["average_price"] || 0
BuyNowBid.create(bid_id: #bidd.id, lowest_price: #low , average_price: #avg)
end
end
flash[:success] = "bid created."
redirect_to "/users/#{current_user.id}"
else
flash[:warning] = 'please sign in'
redirect_to '/login'
end
end
I figured out a way to do by reading more of the rufus documentation and tinkering with it for a while. I was able to create a method that accessed the rufus scheduler in my initializer.
def restart_jobs
puts 'Stopping rufus'
Rufus::Scheduler.singleton.jobs(:tag => 'main_process').each do |job|
Rufus::Scheduler.singleton.unschedule(job)
end
puts 'Starting rufus'
load "#{Rails.root}/config/initializers/scheduler.rb"
end
end
and im calling that restart jobs method after a bid is created so that the bid a user just created gets added to the scheduler when it starts back up
def create
#bidd = Bid.new(event_id: params[:event_id], user_id: session[:user_id], bid: params[:bid], lowest_price: params[:lowest_price])
if session[:user_id] == current_user.id
#bidd.save
send_message("+13125501444", "test")
restart_jobs
flash[:success] = "bid created."
redirect_to "/users/#{current_user.id}"
else
flash[:warning] = 'please sign in'
redirect_to '/login'
end
end
the only part i changed in the scheduler in the intializer is adding the hash
:tag => 'main_process'
and here it is in the scheduler
unless defined?(Rails::Console) || File.split($0).last == 'rake'
s = Rufus::Scheduler.singleton
s.every '1m', :tag => 'main_process' do
Rails.logger.info "hello, it's #{Time.now}"
Rails.logger.flush

Debugging Database Query with NameError uninitialized constant

I have the below query in my Ruby on rail site. I am new to Ruby on Rails and tring teach myself this coding. It is alot different the php that i know.
The originial line that i edited was: (But i need it to pull from a different table.)
Module.const_get(self.search_type.capitalize).where(where).select(select).limit(limit).order(order).page(page).per_page(10)
My question is How do i intiate the Following Constant variable. or reprogram it to work?
I am recieving the following Error:
Started GET "/searches/23795" for 50.83.98.168 at 2014-04-24 22:00:54 -0500
2014-04-24 22:00:54 INFO -- Processing by SearchesController#show as HTML
2014-04-24 22:00:54 INFO -- Parameters: {"id"=>"23795"}
2014-04-24 22:01:01 INFO -- Completed 500 Internal Server Error in 6716ms
2014-04-24 22:01:01 FATAL --
NameError (uninitialized constant Search::Hotloads):
app/models/search.rb:62:in `search'
app/controllers/searches_controller.rb:41:in `show'
Our database table name is "hotloads"
def searches(page)
where = []
where << PrepareSearch.states("dest", self.dest_states) unless self.dest_states.blank?
where << PrepareSearch.date('created_at', self.date_posted, '>=') unless self.date_posted.blank?
if self.search_type == 'load'
select = "loads.id, origin, dest, pickup, delivery, ltl, equipment_id, weight, length, rate"
where << PrepareSearch.attribute('ltl', self.ltl, '=') unless self.ltl.blank?
elsif self.search_type == 'truck'
select = "trucks.id, origin, dest, available, expiration, equipment_id, comments"
where << PrepareSearch.date('available',self.available,self.available_operator) unless self.available.blank?
self.expiration.blank?
end
where << "covered=false AND deleted=false"
where = where.join(' AND ')
order = self.order_by ? self.order_by + " desc" : ""
limit = "LIMIT=200"
Hotloads.where(where).select(select).limit(limit).order(order).page(page).per_page(5)
end
UPDATE
if i change this line to this
Module.Hotloads.where(where).select(select).limit(limit).order(order).page(page).per_page(5)
I get this error
Started GET "/searches/23795" for 50.83.98.168 at 2014-04-24 22:14:15 -0500
2014-04-24 22:14:16 INFO -- Processing by SearchesController#show as HTML
2014-04-24 22:14:16 INFO -- Parameters: {"id"=>"23795"}
2014-04-24 22:14:20 INFO -- Completed 500 Internal Server Error in 3913ms
2014-04-24 22:14:20 FATAL --
NoMethodError (undefined method `Hotloads' for Module:Class):
app/models/search.rb:94:in `searches'
app/controllers/searches_controller.rb:42:in `show'
Requested Controller
class SearchesController < ApplicationController
load_and_authorize_resource
attr_accessor :log
def new
#search = Search.new
end
def edit
#search = Search.find(params[:id])
#search.origin_cs = #search.origin.cs unless #search.origin.nil?
#search.dest_cs = #search.dest.cs unless #search.dest.nil?
%w[pickup delivery available expiration].each do |date|
date = date.to_sym
#search[date] = #search[date].strftime("%F") unless #search[date].blank?
end
end
def create
#search = Search.new(params[:search])
if #search.save
redirect_to #search
else
render 'new'
end
end
def update
#search = Search.find(params[:id])
if #search.update_attributes(params[:search])
redirect_to #search
else
render 'edit'
end
end
def show
#search = Search.find(params[:id])
#searches = Search.find(params[:id])
#results = #search.search(params[:page])
#resultss = #searches.searches(params[:page])
#search.update_attribute(:results, #results.count)
#searches.update_attribute(:resultss, #resultss.count)
respond_to do |format|
format.html
format.js {render "results"}
format.js {render "resultss"}
end
end
def load_search
#search = Search.new
#user_id = params[:user_id]
respond_to do |format|
format.html { raise "Unsupported" }
format.js
end
end
def truck_search
#search = Search.new
#user_id = params[:user_id]
respond_to do |format|
format.html { raise "Unsupported" }
format.js
end
end
def recent_search
#user_id = params[:user_id] || current_user.id
#searches = Search.includes(:origin, :dest).where(user_id: #user_id).limit(15).order("searches.updated_at desc")
respond_to do |format|
format.html { render "_recent_search", locals: {user_id: #user_id, searches: #searches} }
format.js
end
end
def saved_search
#user_id = params[:user_id] || current_user.id
#searches = Search.includes(:origin, :dest).where(user_id: #user_id, saved: true).order("searches.updated_at desc").page(params[:saved_search]).limit(15).per_page(15)
respond_to do |format|
format.html { render "_saved_search", locals: {user_id: #user_id, searches: #searches} }
format.js
end
end
def ezsearch
#user_id = params[:user_id]
respond_to do |format|
format.html { raise "Unsupported" }
format.js
end
end
def states
results = []
JSON(AUTOCOMPLETE_STATES).each {|state| results << state if (state["name"].downcase.include?(params[:q]) or state["id"].downcase.include?(params[:q]))}
respond_to do |format|
format.json { render json: (JSON.dump results)}
end
end
def convert
#search_params = Search.find(params[:search_id])
if #search_params.search_type == "truck"
redirect_to :controller => "trucks", :action => "new", :search_id => params[:search_id]
else
redirect_to :controller => "loads", :action => "new", :search_id => params[:search_id]
end
end
#log = Logger.new("#{Rails.root}/log/searches.log")
#log.datetime_format = "%F %T"
end
Search Model
class Search < ActiveRecord::Base
attr_accessible :available, :delivery, :dest, :length, :ltl, :origin, :pickup, :rate, :search_type, :user_id, :weight, :expiration, :pickup_operator, :delivery_operator, :expiration_operator, :available_operator, :length_operator, :rate_operator, :weight_operator, :origin_zip, :dest_zip, :origin_radius, :dest_radius, :saved, :date_posted, :order_by, :origin_cs, :results, :resultss, :dest_cs, :origin_states, :dest_states, :origin_id, :dest_id, :equipment_ids, :temp_origin, :temp_dest, :temp_equipment
attr_accessor :origin_cs, :dest_cs, :log
before_validation :convert_cs_to_id
OPERATORS = ["<=","<",">=",">","=","!="]
NUMERICALITY_MESSAGE = "Needs to be a number"
LOCATION_FORMAT_MESSAGE = "Location must be in format (city,ST)"
LOCATION_PRESENCE_MESSAGE = "Location does not exist"
validates_inclusion_of :pickup_operator, in: OPERATORS
validates_inclusion_of :delivery_operator, in: OPERATORS
validates_inclusion_of :expiration_operator, in: OPERATORS
validates_inclusion_of :available_operator, in: OPERATORS
validates_inclusion_of :length_operator, in: OPERATORS
validates_inclusion_of :rate_operator, in: OPERATORS
validates_inclusion_of :weight_operator, in: OPERATORS
validates_inclusion_of :search_type, in: ["load","truck"]
validates_numericality_of :rate, {allow_nil: true, message: NUMERICALITY_MESSAGE}
validates_numericality_of :origin_radius,{allow_nil: true, message: NUMERICALITY_MESSAGE}
validates_numericality_of :dest_radius, {allow_nil: true, message: NUMERICALITY_MESSAGE}
validates_numericality_of :weight, allow_nil: true, message: NUMERICALITY_MESSAGE
validates_numericality_of :length, allow_nil: true, message: NUMERICALITY_MESSAGE
validates_presence_of :search_type
validates_presence_of :origin_id, :if => :origin_cs?
validates_presence_of :dest_id, :if => :dest_cs?
belongs_to :user
has_and_belongs_to_many :equipment
belongs_to :origin, class_name: "Location", foreign_key: :origin_id
belongs_to :dest, class_name: "Location", foreign_key: :dest_id
def search(page)
where = []
where << PrepareSearch.states("dest", self.dest_states) unless self.dest_states.blank?
where << PrepareSearch.states("origin", self.origin_states) unless self.origin_states.blank?
where << PrepareSearch.location('origin', self.origin.coords, self.origin_radius) unless self.origin.blank?
where << PrepareSearch.location('origin', self.origin.coords, self.origin_radius) unless self.origin.blank?
where << PrepareSearch.zip('origin', self.origin_zip, self.origin_radius) unless self.origin || self.origin_zip.blank?
where << PrepareSearch.location('dest', self.dest.coords, self.dest_radius) unless self.dest.blank?
where << PrepareSearch.zip('dest', self.dest_zip, self.dest_radius) unless self.dest || self.dest_zip.blank?
where << PrepareSearch.equipment(self.equipment_ids) unless self.equipment_ids.blank?
where << PrepareSearch.date('created_at', self.date_posted, '>=') unless self.date_posted.blank?
if self.search_type == 'load'
select = "loads.id, origin, dest, pickup, delivery, ltl, equipment_id, weight, length, rate"
where << PrepareSearch.date('pickup', self.pickup, self.pickup_operator) unless self.pickup.blank?
where << PrepareSearch.date('delivery', self.delivery, self.delivery_operator) unless self.delivery.blank?
where << PrepareSearch.attribute('length', self.length, self.length_operator) unless self.length.blank?
where << PrepareSearch.attribute('rate', self.rate, self.rate_operator) unless self.rate.blank?
where << PrepareSearch.attribute('weight', self.weight, self.weight_operator) unless self.weight.blank?
where << PrepareSearch.attribute('ltl', self.ltl, '=') unless self.ltl.blank?
elsif self.search_type == 'truck'
select = "trucks.id, origin, dest, available, expiration, equipment_id, comments"
where << PrepareSearch.date('available',self.available,self.available_operator) unless self.available.blank?
where << PrepareSearch.date('expiration',self.expiration,self.expiration_operator) unless self.expiration.blank?
end
where << "covered=false AND deleted=false"
where = where.join(' AND ')
order = self.order_by ? self.order_by + " desc" : ""
limit = "LIMIT=200"
Module.const_get(self.search_type.capitalize).where(where).select(select).limit(limit).order(order).page(page).per_page(10)
end
def searches(page)
where = []
where << PrepareSearch.states("dest", self.dest_states) unless self.dest_states.blank?
where << PrepareSearch.states("origin", self.origin_states) unless self.origin_states.blank?
where << PrepareSearch.location('origin', self.origin.coords, self.origin_radius) unless self.origin.blank?
where << PrepareSearch.location('origin', self.origin.coords, self.origin_radius) unless self.origin.blank?
where << PrepareSearch.zip('origin', self.origin_zip, self.origin_radius) unless self.origin || self.origin_zip.blank?
where << PrepareSearch.location('dest', self.dest.coords, self.dest_radius) unless self.dest.blank?
where << PrepareSearch.zip('dest', self.dest_zip, self.dest_radius) unless self.dest || self.dest_zip.blank?
where << PrepareSearch.equipment(self.equipment_ids) unless self.equipment_ids.blank?
where << PrepareSearch.date('created_at', self.date_posted, '>=') unless self.date_posted.blank?
if self.search_type == 'load'
select = "loads.id, origin, dest, pickup, delivery, ltl, equipment_id, weight, length, rate"
where << PrepareSearch.date('pickup', self.pickup, self.pickup_operator) unless self.pickup.blank?
where << PrepareSearch.date('delivery', self.delivery, self.delivery_operator) unless self.delivery.blank?
where << PrepareSearch.attribute('length', self.length, self.length_operator) unless self.length.blank?
where << PrepareSearch.attribute('rate', self.rate, self.rate_operator) unless self.rate.blank?
where << PrepareSearch.attribute('weight', self.weight, self.weight_operator) unless self.weight.blank?
where << PrepareSearch.attribute('ltl', self.ltl, '=') unless self.ltl.blank?
elsif self.search_type == 'truck'
select = "trucks.id, origin, dest, available, expiration, equipment_id, comments"
where << PrepareSearch.date('available',self.available,self.available_operator) unless self.available.blank?
where << PrepareSearch.date('expiration',self.expiration,self.expiration_operator) unless self.expiration.blank?
end
where << "covered=false AND deleted=false"
where = where.join(' AND ')
order = self.order_by ? self.order_by + " desc" : ""
limit = "LIMIT=200"
Module.Hotloads.where(where).select(select).limit(limit).order(order).page(page).per_page(5)
end
module PrepareSearch
def PrepareSearch.equipment(equipments)
eq = ""
equipments.each {|e| eq += "'#{e}'," }
eq = eq[0..-2]
"equipment_id IN (#{eq})"
end
def PrepareSearch.attribute(attribute, value, comparison)
"#{attribute} #{comparison} #{value}"
end
def PrepareSearch.date(type, date, comparison)
if comparison == '='
"CAST(#{type} as TEXT) LIKE '%#{date.strftime("%F")}%'"
elsif comparison == '<='
"#{type} #{comparison} '#{date.strftime("%F")} 23:59:59'"
else
"#{type} #{comparison} '#{date.strftime("%F")}'"
end
end
def PrepareSearch.zip(type, zip, radius)
location = Location.where(zip: zip).first
if location
"(ST_Distance(#{type}, ST_GeographyFromText('POINT( #{location.coords.x} #{location.coords.y} )')) <= #{radius.to_f*1609.334})"
else
#if no locations match zip search for location that will never exist.
"(ST_Distance(#{type}, ST_GeographyFromText('POINT( 1 1 )')) <= #{radius.to_f*1609.334})"
end
end
def PrepareSearch.location(type, location, radius)
"(ST_Distance(#{type}, ST_GeographyFromText('POINT( #{location.x} #{location.y} )')) <= #{radius.to_f*1609.334})"
end
def PrepareSearch.states(type, states)
states = states.split(",")
st = ""
states.each {|s| st += "'#{s}'," }
st = st[0..-2]
"#{type}_state IN (#{st})"
end
end
#log = Logger.new("#{Rails.root}/log/searches.log")
#log.datetime_format = "%F %T"
private
def convert_cs_to_id
temp_origin = Location.where(cs: self.origin_cs.downcase) unless self.origin_cs.blank?
self.origin_id = temp_origin.blank? ? "" : temp_origin.first.id
temp_dest = Location.where(cs: self.dest_cs.downcase) unless self.dest_cs.blank?
self.dest_id = temp_dest.blank? ? "" : temp_dest.first.id
end
def origin_cs?
errors.add(:origin_cs, "not a valid location") if !origin_cs.blank? && origin_id.blank?
origin_cs.blank? ? false : true
end
def dest_cs?
errors.add(:dest_cs, "not a valid location") if !dest_cs.blank? && dest_id.blank?
dest_cs.blank? ? false : true
end
end
I think you should only do following while querying:
Hotloads.where(where).select(select).limit(limit).order(order).page(page).per_page(5)
as, what I guess, Module.const_get(self.search_type.capitalize) must be returning you the active record model to search, and on a separate note as par ActiveRecord conventions if your database table name is hotloads, than corresponding class name will be Hotload, so in that case, your query will be like: Hotload.where(where).select(select)...

Ruby on Rails filter array using three fields

I am trying to search through my model using 3 columns. Also if the column is empty, it is valid. This is how I am doing it
def getactivityfortoday
#temp = params[:temp]
logger.debug "params temp:#{#temp.inspect}"
#sky = params[:sky]
#day = params[:day]
#todaysactivities = []
#activities=[]
#finaldata = []
#activities = Weatherclockactivity.all
#attemptactivities = []
#attemptactivities = #user.attempts
for activity in #activities do
logger.debug "activity: #{activity.attributes.inspect}"
if #temp.to_i < activity.temperatureMax.to_i && #temp.to_i > activity.temperatuureMin.to_i
if #sky == activity.sky || activity.sky == ""
if #day == activity.day
#todaysactivities << activity
end
end
end
end
for activity in #todaysactivities
for attempt in #attemptactivities
if attempt == activity
finaldata << {activity: activity, attempt: "yes"}
else
finaldata << {activity: activity, attempt: "no"}
end
end
end
respond_to do |format|
format.html { render action: "new" }
format.json { render json: #finaldata }
end
The response I get is an empty array but I should be getting 3 rows as a response.
spelling mistake here
activity.temperatuureMin.to_i
And
finaldata << {activity: activity, attempt: "yes"}
should be
#finaldata << {activity: activity, attempt: "yes"}
Also you could be more concise
def getactivityfortoday
#temp = params[:temp]
logger.debug "params temp:#{#temp.inspect}"
#sky = params[:sky]
#day = params[:day]
#activities = Weatherclockactivity.all
#attemptactivities = #user.attempts
#finaldata = #activities.map do |activity|
if (activity.temperatureMin.to_i + 1...activity.temperatureMax.to_i).include?(#temp.to_i) && ( #sky == activity.sky || activity.sky == "") && #day
#attemptactivities.include?(activity) ? {activity: activity, attempt: "yes"} : {activity: activity, attempt: "no"}
end
end.compact
respond_to do |format|
format.html { render action: "new" }
format.json { render json: #finaldata }
end
end
How about something like this?
I tried to make it a balance of readability and conciseness. First we filter for the desired activities. Then we structure the output. This should be easier to debug.
def getactivityfortoday
#temp = params[:temp].to_i
#sky = params[:sky]
#day = params[:day]
#activities = Weatherclockactivity.all
#attemptactivities = #user.attempts
selected_activities = #activities.select do |activity|
# Make sure it's the right temperaure
return false unless (activity.temperatureMin.to_i + 1 ... activity.temperatureMax.to_i).include? #temp
# Make sure the sky matches, or the sky is blank
return false unless (#sky.blank? || #sky.activity == activity.sky)
# Make sure the day matches
return false unless #day == activity.day
# Otherwise, it's good!
return true
end
selected_attempted_activities = selected_activities.map do|activity|
ret = {activity: activity}
ret[:attempt] = #attemptactivities.include?(activity) ? "yes" : "no"
ret
end
respond_to do |format|
format.html { render action: "new" }
format.json { render json: selected_attempted_activities }
end
end
There are a few typos in your original (for instance, #finaldata not finaldata). Make sure that you spell instance variables (things starting with #, like #sky) correctly, since if you try to access an undefined instance variable, it'll silently default to nil.
The best and flexible way is to use ActiveModel::Model
It allows you to use many more useful methods.
it will seems like:
app/models/activity_report.rb
Class ActivityReport
include ActiveModel::Model
attr_accessor :day, :activity # and etc.
validates :day, presence: true
def day
#day.to_s # for example
end
def day=(value)
#day = value - 1.month # for example every date which user set will set on one month ago
end
# and etc
end
app/controllers/posts_controller.rb
...
def index
#activity = ActivityReport.new(params[:activity])
end
def create
#activity.create!
end
...
app/views/posts/index.html.haml
= form_for #activity do |f|
= f.day
For more information you could take a look at:
http://edgeapi.rubyonrails.org/classes/ActiveModel/Model.html
http://railscasts.com/episodes/219-active-model (old)
http://railscasts.com/episodes/416-form-objects (newer, but a little complex)

Resque require 'csv' not working

I am using resque to process a file in the background. It's a CSV file however I get the following error: uninitialized constant ImportFileHelper::CSV
I have tried to require 'csv' and also include CSV neither will work.
require 'csv'
module ImportFileHelper
HOST = ""
USER_NAME = ""
PASSWORD = ""
def self.process_file(file_data, file_name)
init
#file_name = file_name
begin
csv = CSV.parse(file_data, :headers => true)
csv.each do |row|
#first_name = row["FirstName"]
#last_name = row["LastName"]
#email = row["Email"]
#password = "ch#ngeM3!"
#user_group_name = row["GroupName"].split(",")
#store_name = row["StoreName"]
#external_id = row["ExternalID"]
add_user unless #first_name.nil? || #last_name.nil? || #email.nil? || #password.nil? || #first_name.empty? || #last_name.empty? || #email.empty?
end
rescue NoMethodError => no_method_error
log_error_to_db no_method_error
rescue IOError => error
log_error_to_db error
#errors << error.to_s
rescue Exception => ex
log_error_to_db ex
end
prep_soap_responses_for_output
end
def self.init
HTTPI.log = false
#body = { username: USER_NAME, password: PASSWORD }
#errors = []
#existing_users = []
configure_savon
get_all_groups
get_all_stores
end
def self.prep_soap_responses_for_output
[#existing_users, #errors]
end
def self.log_error_to_db(error)
error.backtrace ||= "Not Available"
if error.message.length > 250
error_message = "There was an error"
else
error_message = error.message
end
ErrorLog.create(message: error_message, trace: error.backtrace, file_name: #file_name)
end
def self.get_store_id
#store_id = #stores[#store_name.to_sym]
end
def self.get_all_stores
#stores = { }
client = Savon::Client.new(HOST + "Storews.asmx?wsdl")
body_data = { mall_id: 1, is_return_offline_store: :false }
#body.merge! body_data
begin
response = client.request :get_store_list, body: #body
if response
hash_response = response.to_hash
stores = hash_response[:get_store_list_response][:get_store_list_result][:store]
stores.each do |s|
store = { s[:name].to_sym => s[:store_id] }
#stores.merge! store
end
end
rescue Savon::Error => ex
log_error_to_db error
#errors << error.to_s
end
end
def self.create_adbuilder_user_object
AdbuilderUser.new(#first_name, #last_name, #email, #user_id, #store_id, #store_name, #user_group_name, #group_id, #external_id)
end
def self.configure_savon
Savon.configure do |configure|
configure.log = false
end
end
def self.add_user
body_data = { first_name: #first_name, last_name: #last_name, user_password: #password, email: #email, external_id: #external_id }
#body.merge! body_data
begin
client = Savon::Client.new(HOST + "UserWS.asmx?wsdl")
response = client.request :add_user, body: #body
if response
#user_id = response.body[:add_user_response][:add_user_result]
if #user_group_name
get_group_id
end
if #store_name
#store_id = get_store_id
unless #store_id.to_s =~ /^0$/
adbuilder_user = create_adbuilder_user_object
UserMailer.create_password(adbuilder_user).deliver if adbuilder_user
end
end
end
rescue Savon::Error => error
log_error_to_db error
if error.message == "(soap:Client) 3: A user with the same email login already exists. Please choose a different login."
#existing_users << #email
else
#errors << error.to_s
end
rescue Exception => error
log_error_to_db error
#errors << error.message.to_s
end
end
def self.get_group_id
begin
#user_group_name.each do |group_name|
user_group_id = #groups_info[group_name.downcase.to_sym]
add_user_to_group user_group_id if user_group_id
end
rescue Exception => error
log_error_to_db error
#errors << error.message.to_s
end
end
def self.get_all_groups
#groups_info = {}
begin
client = Savon::Client.new(HOST + "Usergroupws.asmx?wsdl")
response = client.request :get_user_group_list, body: #body
if response
group = response.to_hash
groups = group[:get_user_group_list_response][:get_user_group_list_result][:user_group]
groups.each do |g|
new_hash = { g[:name].gsub(/\s/, "_").downcase.to_sym => g[:user_group_id] }
#groups_info.merge! new_hash
end
end
rescue Savon::Error => error
log_error_to_db
#errors << error.to_s
end
end
def self.add_user_to_group(group_id)
body_data = { user_id: #user_id, user_group_id: group_id }
#body.merge! body_data
begin
client = Savon::Client.new(HOST + "Usergroupws.asmx?wsdl")
response = client.request :add_user_to_group, body: #body
rescue Savon::Error => error
log_error_to_db error
#errors << error.to_s
end
end
end
So as a work around for this I am doing the csv parsing in the resque job file. This is now allowing it to run. Not sure if this is the best way to do it though.
class ProcessFile
#queue = :rts_file_parser
def self.perform(file_data, file_name)
csv = CSV.parse(file_data, :headers => true)
csv.each do |row|
row_data = { first_name: row["FirstName"], last_name: row["LastName"], email: row["Email"], password: "ch#ngeM3!", user_group_name: row["GroupName"].split(","), store_name: row["StoreName"], external_id: row["ExternalID"] }
ImportFileHelper.process_file file_name, row_data
end
end
end
Mind if I claim the answer (via my comment)?
It looks like it might be a scope resolution issue.
Try ::CSV instead of CSV.
Try adding the gem to the gemfile.

Why are Rails cookies disappearing in Functional Tests

In my Application Controller I am setting cookies in 3 places. In a before filter...
def set_abingo_identity
# skip bots
if request.user_agent =~ /#{#robot_regular_expression}/i
Abingo.identity = "robot"
elsif current_user
Abingo.identity = cookies[:abingo_identity] ||=
{ :value => current_user.id, :expires => 1.year.from_now }
else
Abingo.identity = cookies[:abingo_identity] ||=
{ :value => rand(10 ** 10), :expires => 1.year.from_now }
end
end
... and in two methods...
def remember_visit url
20.times do |i|
# Add cookie to referrer list
name = "referrer#{i}"
if cookies[name].blank?
cookies[name] = {:value => url, :expires => Time.now + 1.year }
break
elsif cookies[name] == url
break # skip
end
end
end
... and this one...
def set_referral_cookie val
ref_name = "referral_id"
offer_name = "offer_id"
cur = cookies[ref_name]
offer = cookies[offer_name]
affiliate = Affiliate.find_by_name val
if cur.blank? || offer.blank?
if affiliate
if cur.blank?
cookies[ref_name] = { :value => affiliate.id.to_s, :expires => Time.now + 1.year }
end
if offer.blank? && affiliate.offer? && !affiliate.expired?
cookies[offer_name] = { :value => affiliate.id.to_s, :expires => Time.now + 1.year }
end
end
end
return affiliate
end
Stepping through in the debugger, I see in the before filter, cookies["abingo_identity"] is set. Then in remember_visit(), both it and cookies["referrer0"] are set, and then in set_referral_cookie(), all 3 are set.
But when I get back to my functional test, only cookies["abingo_identity"] is set.
The cookies["abingo_identity"] part is new, and when I comment it out, the other cookies persist.
What is wrong with this code?
Update:
They are not getting deleted, but they are not all copied to the test case's #response.cookies

Resources