Uninitialized constant CreateJob::RestClient - ruby-on-rails

I am trying to get a job to fire off that creates an order in our ERP.
all works just fine directly through the gem, so I started making the job itself.
I have 3 files: erp_order_methods.rb, create_or_update_erp_order.rb order.rb
erp_order_methods.rb:
module ErpOrderMethods
# These methods are used by create_or_update_erp_order_job
def self.include base
bese.extend ClassMethods
end
module ClassMethods
create_or_update_erp_order_job.rb
class CreateOrUpdateErpOrderJob
include ErpOrderMethods
#queue = :priority_queue
def self.perform(task_id, order_id)
task = Task.find(task_id)
order = Order.find(order_id)
erp_order = order.erp_order
order.rb (model)
def create_or_update_erp_order
#task = Task.create(
status: "scheduled",
description: "Create or Updat Order for Web Order No: #{self.id}",
system_task: true
)
Resque.enqueue(CreateOrUpdateErpOrderJob, #task.id, self.id)
end
When i go to test this, I am getting:
General Error: Type - NameError | Message - uninitialized constant CreateOrUpdateErpOrderJob::RestClient

so I found the issue after some digging around.
def self.include base
bese.extend ClassMethods
spelling errors: bese => base
self.include => self.included

Related

How to solve NameError for QueryAssociationColumn on Redmine Plugin?

I'm new to Ruby and Rails and was trying to create a simple patch plugin that includes another plugin's extra property as a column available when listing issues.
Based on what I've read so far I wrote a new file at lib/issue_query_patch.rb containing:
require_dependency 'issue_query'
module IssueQueryPatch
def self.included(base) # :nodoc:
base.send(:include, InstanceMethods)
base.class_eval do
alias_method_chain :available_columns, :story_points
end
end
module InstanceMethods
# Adds the story points column to default the redmine issue query
def available_columns_with_story_points
columns = available_columns_without_story_points
columns << get_story_points_column
return columns
end
def get_story_points_column
return #story_points_column if #story_points_column
#story_points_column = QueryAssociationColumn.new(:agile_data, :story_points, :caption => :label_agile_story_points)
#story_points_column
end
end
end
IssueQuery.send(:include, IssueQueryPatch)
But when I try to list my issues I get:
NameError (uninitialized constant IssueQueryPatch::InstanceMethods::QueryAssociationColumn):
A similar approach using QueryColumn does not raise a NameError.
Since both classes are declared at app/models/query.rb I'm clueless to why this is happening.
How can I get rid of this error?

Rails concern undefined method error

I'm getting an error when trying to call a concern method within a models method. I have several concerns and I've set them up in the same way. The strange thing is that one method works but the other one doesn't. Here is my code.
param_set.rb
class ParamSet < ApplicationRecord
include Searchable
include Comparable
def self.import(file, user_id, group_id = 1, project_id = nil)
param_set_ids_map = {}
yml = YAML.load_file(file)
yml.each do |component, elements|
case component
when 'param_sets'
elements.each do |importing_attributes|
old_param_set_id = importing_attributes.delete('id')
importing_attributes['user_id'] = user_id
importing_attributes['group_id'] = group_id
importing_attributes['project_id'] = project_id
ParamSet.search(User.first, {search: "test"})
ParamSet.compare(importing_attributes)
end
end
end
param_set_ids_map
end
end
comparable.rb
module Comparable
extend ActiveSupport::Concern
module ClassMethods
def compare(importing_attributes)
logger.debug "Works!"
end
end
end
searchable.rb
module Searchable
extend ActiveSupport::Concern
module ClassMethods
def search(current_user, params, visible: true)
results = paged_filter(current_user, params[:scope], params[:page] || 1)
results = results.visible if visible
results.where!("lower(name) LIKE lower(?) OR lower(description) LIKE lower(?)", "%#{params[:search]}%", "%#{params[:search]}%")
if params[:order].blank?
results.order!('updated_at DESC')
else
results.order!(params[:order])
end
results.reverse_order! if params[:reverse] == 'true'
results || []
end
end
end
The error that I get is:
undefined method `compare' for #<Class:0x000056167cb01718>
The method ParamSet.search(User.first, {search: "test"}) works find and doesn't give an error. The method ParamSet.compare(importing_attributes) however gives the error. I don't know what is going on and what the difference is between calling the two concern methods within a model method.
Can anybody explain what is going on?
When you do
include Comparable
you're including the Comparable module of ruby, not your own module. That module doesn't have a compare method.
Try changing your module name...
module Matchable
and the file to matchable.rb
and then
include Matchable
And you should be ok.

Ruby: Avoid long nested module when reference to constant

I have a class that define a constant in my rails app. For example:
module A
module B
class C
CONSTANT = "constant"
end
end
end
And then, in another module, I want to get this constant:
module Test
class Main
def get_constant
const = A::B::C::CONSTANT
end
end
end
This is too long and verbose. I tried some ways for not using prefix A::B::C. for example:
module Test
class Main
include A::B
def get_constant
const = C::CONSTANT
end
end
end
But in all cases, I always meet error because my rails app cannot find this constant. Please tell me how
In the second class/module, you can create a reference to the constant from the first:
class One
OneConst = 1
end
class Two
TwoConst = One::OneConst
def self.two_const
TwoConst
end
def two_const
TwoConst
end
end
puts Two.two_const
puts Two.new.two_const

Undefined class method using Rails Concerns

I did everything pretty much as described here: question
But I keep getting error:
NoMethodError: undefined method `parent_model' for Stream (call 'Stream.connection' to establish a connection):Class
In model/concerns faculty_block.rb
module FacultyBlock
extend ActiveSupport::Concern
included do
def find_faculty
resource = self
until resource.respond_to?(:faculty)
resource = resource.parent
end
resource.faculty
end
def parent
self.send(self.class.parent)
end
end
module ClassMethods
def parent_model(model)
##parent = model
end
end
end
[Program, Stream, Course, Department, Teacher].each do |model|
model.send(:include, FacultyBlock)
model.send(:extend, FacultyBlock::ClassMethods) # I added this just to try
end
In initializers:
require "faculty_block"
method call:
class Stream < ActiveRecord::Base
parent_model :program
end
It seems that the Stream is loaded before loading concern, make sure that you have applied the concerns inside the class definition. When rails loader matches class name for Stream constant, it autoloads it before the finishing evaliation of the faculty_block, so replace constants in it with symbols:
[:Program, :Stream, :Course, :Department, :Teacher].each do |sym|
model = sym.to_s.constantize
model.send(:include, FacultyBlock)
model.send(:extend, FacultyBlock::ClassMethods) # I added this just to try
end

I would like to know how can I force a model loading in development environment

I would like to know how can I force a model loading in development environment. (RAILS 2.3.10)
When I am in test or development mode, SomeProject is never initiated until used. Here is what the console says:
> project = Project.find(:some)
RuntimeError: Can not find 'some'
from /path/to/rails/lib/identifier.rb:23:in `class_from_key'
from /path/to/rails/lib/identifier.rb:8:in `find'
from (irb):4
> Projects::SomeProject
=> Projects::SomeProject
> project = Project.find(:some)
=> #<Projects::SomeProject:0x10810ceb8 #key="some", #full_name="Some Project">
The identifier method is never called. However, it does work in production mode and when I cache classes in development mode.
config.cache_classes = true
But that just eliminates the benefits of development.
Any ideas on a nice way to make sure that all the subclasses are auto loaded?
Code Details
I have a Project class
class Project
extend Identifier
def full_name
#full_name
end
def key
#key
end
end
that is the base class for more specific project classes
class Projects::SomeProject < Project
identifier :some
def initialize
#key = 'some'
#full_name = "Some Project"
end
end
Projects extend Identifier module which should self register the sub classes and expose a find method to instantiate by a key:
module Identifier
def find(*args)
key = args.shift
klass = class_from_key(key)
if args.empty?
klass.new
else
klass.new args
end
end
private
def class_from_key(key)
raise "Can not find '#{key}'" unless identifiers.has_key?(key)
identifiers[key]
end
def identifiers
##identifiers ||= {}
end
def identifier(key)
identifiers[key] = self
end
end
It might not hurt to add
require 'some_project'
at the top of your file if you have such needs.

Resources