Making my first gem - Where do I start? - ruby-on-rails

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.

Related

Rails Spree internals

I am trying to use Spree with my RoR application. Ok, I do follow all those guides and FAQs on official website when I want to customize something. That's ok and no problem with it. One question, to which I could not find a clue -- how is that possible, that there is nothing in apps/view, apps/models folders, but it's still working? I mean, yes, I can create something in these folders and redefine the behavior of my views (actually, this is one of the ways of customization), but I really want to understand the internals. I am pretty new to Rails and got used to classic app folder structure.
what you are wondering about is the magic of Rails Engines.
Ruby on Rails allows you to define Engines (your app is one too) and when it looks for views/controllers/etc.. all mounted engines are part of the search path.
So the view is inside the Spree gem, not visible to you - but it still looks in there.
If you put something in your view folder with the same name, it will take precedence over the views in the Rails engine you have in the Gem.
Here is a good guide on how Engines work in Rails:
http://edgeguides.rubyonrails.org/engines.html
One good example of these Engines is the jQuery-rails Gem you probably use inside your Application.
It has no code at all (except for some fallbacks for Rails 3.0 and below that don't have an asset pipeline), but the jQuery.js file in the app/assets/javascripts folder. And since the engine is in the load path you can require the asset that's in there..
The engine itself has the same folder structure as your app (app/views, app/controllers ...)
You can look at the internal structure of Spree here: https://github.com/spree/spree/tree/master/core/app

Bad idea to alter external gem files in rails so I can use CSS in app?

Did some research on how to style the views of the devise gem, didn't really find much so added some html and a render partial to the external libray to assist with styleing and to continue to offer my users some links. I get this gut instinct that it isn't the right way to do things however. Any online references provided would be greatly appreciated!
It depends on how you did it.
If you just edited the installed gem source, then perhaps that wasn't ideal.
But if you forked the Devise github repo, like 1,270 people have done as of today, you could easily track changes to the source and merge them with your version, contribute your changes back if you wanted, and you could build a gem of your own that you could use in style.

rails i18n, the best way to manage localization

I'm using http://guides.rubyonrails.org/i18n.html for localization. Looks like that the only solution for updating localization is to update yml files. May be there are some solutions or gems which adds ability to update localization via application. For example we can include some gem and update localization files from our app. Is there some solutions for that?
Shameless plug for my own product here, but check out http://www.localeapp.com for a solution that works with YAML files out of the box.
Take a look at Globalize 3. It stores the translation/localization data in the database. There's also a gem to hook it up with ActiveAdmin.
Also, while you're at it, don't miss the rails-i18n gem mentioned in the guide. It translates Rails itself into many languages, so you don't have to do it yourself.
There is a great railscast to manage i18n through Redis.

less-rails-bootstrap ... where are the less files

I don't know if this is a ridiculous question however I am creating a website with RefineryCMS, Ruby, and Rails. I chose to speed up some things by using Twitter's Bootstrap via the less-rails-bootstrap gem. I do however need to customize some colors and such though I can't seem to locate any of the less files. I looked in the logs and see references to things such as 'twitter/bootstrap.css' though I'm not able to locate this or where the less files live.
Where are these things? Else, how to do you override the defaults?
When you complete with gem install and bundle install command , you should :
rails g bootstrap:install
This will insert some files in your app/assets dir , including bootstrap_and_overrides.css.less in app/assets/stylesheets.
You can learn more in this Railscast.
UPDATE: (Jan 25 2014): Valuable information form the comment of #Niels Abildgaard:
According to GitHub repository of less-rails-bootstrap gem, Rails generator command shoud be :
rails generate less_rails_bootstrap:custom_bootstrap

Rails gem with CSS and javascript

I've got a simple rails gem (created using bundler) and I'd like to extend it by adding some CSS and javascript functionality. However, I'm unsure how to go about this and where to add the files. In particular, I need need more information on how it all fits together with the asset pipeline once it gets included in another project.
Can anyone give me the lowdown on how this works and either provide some simple examples or link to a tutorial? Literally 1 css and 1 js file is all I'm looking to include. Thanks.
You could write the gem as an engine. This allows you to have an app folder in the gem just as any Rails application would have. You can add models, views, controllers, assets etc.
Once you have it set up it's quite intuitive and it's a familiar way to create a gem if you're used to creating Rails apps.
This should get you started:
http://coding.smashingmagazine.com/2011/06/23/a-guide-to-starting-your-own-rails-engine-gem/

Resources