Ruby on Rails: where to put the 'require' files? - ruby-on-rails

I'm trying to use this gem barometer and in the document it says that it can be used right out of the box with using require 'barometer'
I've always used gems and put them in the gemfile, but I think this is different...
Do I just download this entire repo, and copy all the files in the lib folder into my vendor folder? or maybe public folder?
Where would you typically put these files? And where would you include the require? Should this be in the application controller? Or maybe in the helper? Sorry for this really noob question.
I know in my local environment, I can just type in gem install barometer in my console, and not have to put in a require, but I don't think this will work in heroku, or production environment.

I've always used gems and put them in the gemfile, but I think this is different...
No, this is no different. Barometer is a Rubygem and putting it in your Gemfile is exactly the way to use it.
As with every library, your require should go in whichever file uses the code, for example the same file that the Barometer.new call is. You don't always need the require line depending on your Ruby environment, but it's always a good idea to get used to it

Related

How to find and modify required assets in a rails project?

I have //= require jquery.ui.selectable in application.js. Works great, well sort of. I need to customize this method because the fieldset selector is conflicting in my app.
// This is a manifest file that'll be compiled into application.js,
which will include all the files listed below.
Where is the file actually stored so that I can modify it?
Assuming you didn't include that file yourself, you probably have something like gem 'jquery-ui-rails' in your Gemfile. To directly answer your question, the jquery UI assets (and indeed any assets belonging to a gem) will all be stored in their respective gem directories, which will be dictated by your $GEM_HOME environment variable.
For example, in my case, on my Mac using RVM, the location of my jquery UI directory is:
/Users/paul/.rvm/gems/ruby-2.0.0-p247#some_application/gems/jquery-ui-rails-4.0.4
with $GEM_HOME being /Users/paul/.rvm/gems/ruby-2.0.0-p247#some_application.
Having said that, I would strongly not recommend manually altering the selectors in the javascript files of a gem. The easiest and cleanest approach would be to alter your DOM structure, at least changing the class, id, or other selectable attributes, in such a way as to avoid the conflict you mentioned. If you update your post with more details regarding the conflicting selector, I'm sure we can help you in a much better way.
if jquery.ui.selectable is not included via gem, it will be located in the following places
app/assets/javascripts/jquery.ui.selectable.js
lib/assets/javascripts/jquery.ui.selectable.js
vendor/assets/javascripts/jquery.ui.selectable.js
vendor/assets/somepackage/jquery.ui.selectable.js
if it is in gem , you can just copy jquery.ui.selectable.js file from gem repo and customise it and put it in assets folder.
NOTE: i assumed js, it can also be coffee.js

Why is there Rails.rb files all over the place?

Was digging around my Rails applications and noticed that there are rails.rb files all over the place. In my ruby gems directories like:
...gems\devise-2.0.4\lib\devise\rails.rb
...gems\cucumber-rails-1.3.0\lib\cucumber\rails.rb
...gems\railties-3.2.3\lib\rails.rb
I am assuming that there are executed whenever you issue some command like "rails xxx". So all these extra rails.rb files combine with the original rails.rb file to essentially make one big rails.rb file. Essentially, when we type in "rails xxx" it goes thru all them all?
Just looking for some confirmation PLUS a little more knowledge about this. Thanks.
The best way to understand what these rails.rb files are doing, is to read the source code.
ralties
devise
cucumber-rails
As you can see, in any library the file assumes a different scope. The common behaviour is that the file rails.rb normally contains the code required to initialize the library when loaded from a Rails project.
BTW, this has nothing to do with the script/rails command and there is no "big rails.rb" file.
The files are not generated but are simply source files of these libraries you are using.
In this case they are probably rails-related classes that either extend Rails in some way or modify it or make the library interact with Rails.
Rails is a very common framework in Ruby land so most if not all libraries will have some sort of integration with Rails.
By no means are all of those loaded when you run rails XXX but rather when your application loads these libraries their rails.rb files may be executed to provide some sort of integration with Rails.

Making my first gem - Where do I start?

So I have this idea for a RubyGem that I think would be an awesome experience to learn more about Ruby and Rails but...I have no idea where to start.
My idea is to generate a folder "articles" where you can put markdown files. From this folder the main blog page displays only the titles as links to the articles themselves.
It sounds simple but I honestly have no idea where to start. What articles do you recommend I read if I want to insert lines into routes.rb, generate a folder and display markdown in Rails?
I would recommend one of these tutorials for gem creation:
http://net.tutsplus.com/tutorials/ruby/gem-creation-with-bundler/
http://railscasts.com/episodes/245-new-gem-with-bundler
To modify the routes.rb file, you'll just need File.open to read lines in. Use regular expressions to determine where you want to insert your line, and write the file back out.
To create a folder, look at the documentation for Dir.new
For Markdown in Ruby/Rails, I like the rdiscount gem: https://github.com/rtomayko/rdiscount
Railties provide a nice way to do certain things like this. You'll probably use http://api.rubyonrails.org quite a bit. There is some Railtie documentation on that site here: http://api.rubyonrails.org/classes/Rails/Railtie.html.
I recommend reading the RubyGems guides – especially What is a gem?, Make your own gem and Patterns.
Since you're likely already using Bundler, you can run bundle gem <name> to generate a gem project with stuff already in place. It does save work, but refer to the guides if there's something you don't understand.
Also, watch some open source projects on GitHub – observing other developers and taking note of how they do things certainly helps.
The simplest way is probably to read other gems that do anything similar to what you want to accomplish. Start with their .gemspec files that will list all the other files which are needed for the gem to work, and a list of gem dependencies.
Responding more to how to get started with creating gems, the following are 2 popular, documented gems that can help you.
https://github.com/seattlerb/hoe
https://github.com/technicalpickles/jeweler
Also, though it does more than you're trying to do with your gem (it's a static site generator), https://github.com/mojombo/jekyll is a very popular gem which you place .markdown files into a posts/ directory which are converted to static HTML pages via rake. I would imagine you could find at least some functionality you're after there.

Best practices when including Rails models in another application

I'm developing a ruby application that uses the models and data from another Ruby on Rails web application as its main data source.
The Rails models were included in this application by including the environment.rb file in the main file like this:
# Require Rails
require_relative "../../RailsApp/config/environment.rb"
This works but there are uninitialized dependencies when loading models that use gems that are defined in the Rails Gemfile. (For example, acts_as_taggable_on, rack-pjax, devise, etc)
This ruby application dependencies are also managed through Bundler, so at the moment the only way to get the application working is to copy and paste the contents from the Rails' Gemfile into the ruby app's Gemfile.
Obviously this approach is not optimal as the gem requirements are duplicated.
Is there a better way to include Rails and the dependencies that its models require in another application? Is there a way to include a Gemfile into another?
Here are some options, in order of simplicity
Just keep everything in one app, a lot of stuff is easier this way
Use plugins to share common code
Use web services to share data
You could extract the models and code out from RailsAppA into a Gem. RailsAppA then includes that Gem and uses it.
The gem can remain in a private repository and does not need published.
Both apps would then do something like:
gem "yourapp-modelage", git: "http://github.com/you/yourapp-modelage.git"
Then, App2 would also use that Gem... How much goes into the Gem will depends on how much you need to re-use.

What's the best way to require source files when writing a gem

I'm converting the models folder of a rails app into a gem so more rails app can use the same domain model layer.
In the initial rails app, the loading of all model files is handled by activesupport so there no require statement everywhere. But in the gem version, it has to be done manually. I had a look at the code of popular gems such as rspec, factory_girl and state_machine and it looks like they all require all necessary source files in one file, usually named after the project.
The downside of this approach is that you need to maintain one file listing all the others and that seems a bit clumsy. And even though I have hit that problem yet, I can foresee cirular dependency issues.
Another way would be to have each source file requiring the files it needs. That would work in the standalone gem as well as in the rails app. But I haven't seen examples of gems using that technique so I'm wondering if there is a downside I'm not seeing?
thanks
You're talking about model files, if so, simply adopt the same structure as a standard Rails app and make your gem inherit from Engine. Everything will be included painlessly.

Resources