Create a simple Rails 3 text helper Gem -
this question has answer here:
- how extract rails view helpers gem? 3 answers
i've been working on first rails 3 plugin, package simple helper function having in applicationhelper
of apps. can see whole code on github.
here's first attempt:
## lib/semantic_id.rb ## require 'semantic_id/version' module ::actionview::helpers::texthelper def semantic_id string = string.new case when controller.action_name =~ /new|edit/ string += controller.action_name + "_" when controller.action_name =~ /index|create/ string += controller.controller_name else string += controller.controller_name.singularize end string += "_view" end end
now, works, understand not 'rails 3 way' of extending activesupport or other rails module. haven't been able find documentation on how you're "supposed" build rails 3 gem. tried following rails guide, method given there adding helpers didn't work, or else missing something.
my question is: given code above example of functionality i'm looking for, how turn rails 3 plugin gem?
thanks!
the problem you've done you've bound gem's functionality actionview. better make more generic.
to fix should package code in own module , inject module view environment. example, put helper own module (and file):
# lib/semantic_id/helper.rb module semanticid module helper def semantic_id ... end end end
then add actionview using railtie:
# lib/semantic_id.rb require 'semantic_id/helper' activesupport.on_load(:action_view) include semanticid::helper end
you want separate functional part of helper part inserts framework. method contains rails-specific code, if didn't technique allow use sinatra , other frameworks well.
in general, guts of project should in lib/<project_name>/*.rb
, , lib/<project_name>.rb
should make functionality accessible.
Comments
Post a Comment