Ruby on Rails: Using Modules - ruby-on-rails

I am relatively new to Ruby on Rails, but I have almost completed the online course on Lynda. I am stuck on 14-5 "Using the positionMove module". The tutorial is in Rails 3 while my version is Rails 4. I have the solution code, however it does not work for me. I have put my file position_mover.rb in the lib directory. Then in my subject model I require and include the module like this:
require 'lib/position_mover'
class Subject < ActiveRecord::Base
include PositionMover
...
end
I used the methods in this model in the Subject controller just like the instructor. However, when I go to run my app on the Subjects index page I get the error:
cannot load such file -- lib/position_mover
app/models/page.rb:1:in `<top (required)>'
app/views/subjects/list.html.erb:23:in `block in_app_views_subjects_list_html_erb__420960863_60005508'
app/views/subjects/list.html.erb:18:in `_app_views_subjects_list_html_erb__420960863_60005508'
app/controllers/subjects_controller.rb:9:in `index'
I have tried many different approaches that I found online such as adding it to the class definition (Ruby on Rails 4.0 - Loading and using a module) or moving it to a different directory and specifying the path. How do I achieve the same result in Rails 4? What am I doing wrong? Any help is greatly appreciated.

require "#{Rails.root}/lib/position_mover"
or
require_relative 'lib/position_mover'
You also can auto-loading lib files.
in config/application.rb:
config.autoload_paths << Rails.root.join('lib')

Related

Include a custom module on Spree and Rails

I'm trying to add my module to my class_eval on Spree.
This is located on: lib/spree/core/app/models/spree/payment/processing.rb
Tried with the following:
module Spree
Payment.class_eval do
require GatewayError
end
end
I am trying to include the following located on: lib/spree/error_override.rb
module Spree
module GatewayError
end
end
The error I'm getting when I try to load the server is:
`block in <module:Spree>': uninitialized constant Spree::GatewayError (NameError)
Its my first time trying to include my own module to a class, would be awesome if someone can point me in the right direction.
Thank you in advance!
One solution would be to manually require the file during initialization process.
config/initializers/require.rb:
# put here all files that you want to require manually
require "#{Rails.root}/lib/spree/error_override.rb"
And that's it - your module is now ready to use ;)

Rails 3 trouble with namespaces & custom classes (uninitialized constant)

I have a file in my Rails 3.2.11 project called app/queries/visible_discussions.rb which looks like the following:
class VisibleDiscussions
...
end
I'd like to namespace the query so that I can call it using something like Queries::VisibleDiscussions so I tried to do the following:
module Queries
class VisibleDiscussions
...
end
end
However, I'm getting a uninitialized constant Queries (NameError) when I try to call Queries::VisibleDiscussions from the rails console.
Any ideas?
if you add lib to your autoload_paths then it will respect the namespacing under lib - lib/query/visible_discussions.rb
or create a new dir under app - say src and then nest your code there - app/src/query/visible_discussions.rb
i would use the 3rd style in your post for either of these, i.e.
module Query
class VisibleDiscussions
...
end
end
both of these solutions are annoying to me, there might be a way to tell rails to namespace directories under app, but i have no clue how it would be done
Rails needs to know what directories to load (a part from the defaults). Try:
#config.application.rb
config.autoload_paths += %W(#{config.root}/queries)

Rails include module in model trouble

I have module in /lib/models/scopes.rb
module Models
module Scopes
extend ActiveSupport::Concern
...
end
end
I'm trying to include it from model:
class User < ActiveRecord::Base
include Models::Scopes
end
And getting error:
NameError: uninitialized constant User::Models
How to solve this trouble? Maybe it`s wrong to keep this types of files in /lib?
Environment:
Rails v3.1
Ruby v1.9.3
Rails doesn't require files in the lib directory automatically, but you can add to the autoloaded paths in config/application.rb:
config.autoload_paths += %W(#{config.root}/lib)
Restart the server to pick up the new settings.
This will now load the file automatically when the module name is first used. In development mode, you might want to reload the module after every change in order to see the changes without restarting the server. To do that, add it as an eager load path instead:
config.eager_load_paths += %W(#{config.root}/lib)
The scope shouldn't be a problem as long as you don't have a Models class or module within User or anywhere else.
when you define your class, you're "opening" a new scope. So when you do Models::Scopes, ruby is looking for User::Models::Scopes. You can fix this by using ::Models::Scopes, the :: telling ruby to look in the global scope.
FYI: I'm not sure about the terms I used or even if my train of thought if correct; but the solution should be good anyway. I'd think Ruby would try for ::Models::Scope after failing to find User::Models::Scope, but it doesn't.. Maybe there is a User::Models scope defined somewhere? Anyway, as you can see, I'm not yet familiar with those. You might want to dig on the subject if that interests you

How to organize models in Sinatra?

I have a tirable orgnizing my Models in a Sinatra project.
Let's say I have 2 models: Post and Comment, nn Post model, I have to call Comment model. And now I have <class:Post>': uninitialized constant Comment (NameError).
I know its an issue in ordering the requiring for the models, but what about if I have lots of models? What's the Rails way in requiring models, etc.?
UPDATE
I use this code to auto_load my models in Sinatra/Rack/Grape applications. This code should be at the top of your code ie in the boot file.
models = File.join(File.dirname(__FILE__), 'app', 'models') # path to your models
$LOAD_PATH << File.expand_path(models)
# Constent Missing for requiring models files
def Object.const_missing(const)
require const.to_s.underscore
klass = const_get(const)
return klass if klass
end
You should put all of your models in a folder, such as lib in your application, then you can add this to the top of your Sinatra app file:
$: << File.dirname(__FILE__) + "/lib" # Assuming app.rb is at the same level as lib
require 'post'
require 'comment'
You should organise your code so that you do not call other models until all model declarations are loaded.
The Rails way is based on a very nice Ruby feature: const_missing. You could write your const_missing method or looking around the web for a solution with const_missing and sinatra.
no prob when I tried this
a Comment if it is in a method of Post shouldn't be actually evaluated
there must be some circumstance triggering the NameError
don't call Post in the body of the class declaration
load all the model files per the first commenter's suggestion
shouldnt be having the same reference troubles as Java per se
in a dynamic lang like Ruby

Ruby on Rails module require issue

I'm divin to the Rails 3 via some tutorials.
I found usefull the lynda.com/Kevin Skoglund's Rails 3 Essential training.
Close to the end of the course I run into a problem what I can't solve and no reference found on the net (or lynda's site).
There is a position_mover (similar to act_as_list, but simpler) module what I must include to the model's to use.
But I'm getting errors.
This one:
LoadError in SubjectsController#index
no such file to load -- lib/position_mover
Rails.root: C:/Programozas/work/simple_cms
Application Trace | Framework Trace | Full Trace
app/models/subject.rb:1:in `<top (required)>'
app/controllers/subjects_controller.rb:13:in `list'
app/controllers/subjects_controller.rb:8:in `index'
This error occurred while loading the following files:
lib/position_mover
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
I have access to the example files also. I tried to hard copy the whole app, than modify the gems versions to be correct and I get the same error.
The app runs perfectly untill I try to access a model, where the require presented.
A model:
require 'lib/position_mover'
class Subject < ActiveRecord::Base
include PositionMover
has_many :pages
...
end
I'm sure this is a kind of mega easy thing what I can't recognise. Please help to identify the problem!
Yours,
Kael
What if you remove the lib/ part? If the gem is in your Gemfile it is not even necessary to require it.

Resources