How to configure Rails 3.1 app from a gem? - ruby-on-rails

For example, I want to change rails settings when I require my gem to application.
If I simply put this line
Rails.application.config.assets.prefix = '/app/themes/default/assets'
into my gem, this does not change the settings. I think I need to use some rails hooks there.
Please help me guys

You shouldn't need to configure the path of the assets. When put in the proper place, they should load automatically. You need to define an "Engine". Have a look at a gem I made: https://github.com/iain/formalize-rails

Related

How to convert existing rails application to gem

I have a rails application that is kind of similar to the active_admin gem. I want to make this existing application into a gem.
I know that you can generate a gem with:
bundler gem gem_name_here
This command creates an empty gem. Which is fine I suppose, I just don't know where to put all my code.
You're ostensibly supposed to put all of your code in the /lib directory. However, my application has an assets pipeline and an app directory with all my models, controllers, and views. I don't know where to place all of these directories in the gem. Any thoughts or ideas?
If you need me to share any code, I'll gladly share it.
You're describing a rails engine. Engine is a miniature rails application that can be used inside other rails app. For more details see official rails guide

Ruby on Rails - How to set up Ahoy Gem

Im setting up Ahoy gem into my application, but there are some options that I don't understand where to add.
This is what I have done currently:
1) Added gem "ahoy_matey" to my Gem File and ran Bundle install & restarted my server
2) Added //= require jquery & //= require ahoy to my application.js
3) Ran rails generate ahoy:stores:active_record and added new tables to my database by running rake db:migrate
I also have ahoy.rb in my initializers folder.
At this point I can see that ahoy is added in my application, but not sure how to do some stuff.
I want to add Ahoy.cookie_domain = :all & Ahoy.visit_duration = 1.minute, But I don't know where to add them
AND one other issue I have is that whatever page I go to is added to my Visits table (PS: I haven't added any JS or Ruby code for tracking in any pages or controller) and as I see it ,ahoy gem tracks all events/visits in my application. I only want to track events/visits on one particular action in my whole application, posts#show action and rest don't need to be added at all.
How can I achieve this?
I want to add Ahoy.cookie_domain = :all & Ahoy.visit_duration = 1.minute, But I don't know where to add them
You add them in the initializer that you created (./config/initializers/ahoy.rb). This is precisely the purpose of files in that folder: Setting configuration values for the application to use.
whatever page I go to is added to my Visits table [...] I only want to track events/visits on one particular action in my whole application
The Gem's README says:
There are three ways to track events. [...] See Ahoy.js for a complete list of features.
So start there, and take a look at ahoy.js if you need some more advanced configuration. The gem is very flexible - you just need to read the documentation and configure it as needed.

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

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

How do i access constant defined in Rails 3 application in "whenever" gem of schedule.rb file

i have few constants declared in application_controller.rb file.
Is there any way to access them in schedule.rb file of whenever gem?
Regards,
Sudhir C.N.
The whenever gem does not include the Rails environment by default. If you wanted to do this, you would need to load Rails within your cron tasks. This is not recommended however, especially if all you need is access to some constants.
I would recommend moving your constant definitions to a config file or initializer. Then you can require that file from both your application_controller and your whenever configuration. It will consolidate the logic and give you access to those constants without having to load the entire Rails app for each invocation of whenever.
I hope that helps.

How do I get TextMate to use Ruby on Rails by default?

How do I make TextMate use the Rails bundle by default? On some files it's enabled, and on others (including models) it's not, and I wish it would just assume that every .rb file is a Ruby on Rails file unless I tell it otherwise.
Is there a project-level or global setting for this?
It's possible to do it at a global level, but you have to edit and view some plist files to make it work. Check out this site for more information.
As far as I know you have to change the scope selector for each bundle. Maybe TM2 will be different.

Resources