Send this to a friend
rails 2.3.4 doen’t have i18n support in labels
http://lawrencesong.net/2009/04/i18n-label-in-rails-monkey-patch/
1 module ActionView
2 module Helpers
3 class InstanceTag
4 def to_label_tag_with_i18n(text = nil, options = {})
5 text ||= object.class.human_attribute_name(method_name) if object.class.respond_to?(:human_attribute_name)
6
7 to_label_tag_without_i18n(text, options)
8 end
9
10 alias_method_chain :to_label_tag, :i18n
11 end
12 end
13 end
module ActionView
module Helpers
class InstanceTag
def to_label_tag_with_i18n(text = nil, options = {})
text ||= object.class.human_attribute_name(method_name) if object.class.respond_to?(:human_attribute_name)
to_label_tag_without_i18n(text, options)
end
alias_method_chain :to_label_tag, :i18n
end
end
end
Send this to a friend
1
2
3 def snippet(text, wordcount, omission)
4 text.split[0..(wordcount-1)].join(" ") + (text.split.size > wordcount ? " " + omission : "")
5 end
6
7
8
9
10 snippet(@post.body, 50, "#{link_to "More...", @post}")
def snippet(text, wordcount, omission)
text.split[0..(wordcount-1)].join(" ") + (text.split.size > wordcount ? " " + omission : "")
end
snippet(@post.body, 50, "#{link_to "More...", @post}")
Send this to a friend
create this helper and uses link_unless_current_controller
1 def current_action?(options)
2 url_string = CGI.escapeHTML(url_for(options))
3 params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
4 params[:controller] == @controller.controller_name && params[:action] == @controller.action_name
5 end
6
7 def current_controller?(options)
8 url_string = CGI.escapeHTML(url_for(options))
9 params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
10 params[:controller] == @controller.controller_name
11 end
def current_action?(options)
url_string = CGI.escapeHTML(url_for(options))
params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
params[:controller] == @controller.controller_name && params[:action] == @controller.action_name
end
def current_controller?(options)
url_string = CGI.escapeHTML(url_for(options))
params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
params[:controller] == @controller.controller_name
end
Send this to a friend
1 class TabHelper
2 attr_reader :html, :tabs
3
4 def initialize(template, states)
5 @template = template
6 @states = states
7 @html = []
8 @tabs = {}
9 end
10
11 def add(action, text)
12 url = { :action => action }
13 html =
14 if @template.request.path.sub(/\?.*/, '') == @template.url_for(url)
15 @states[:active].call(text, url)
16 else
17 @states[:inactive].call(text, url)
18 end
19 @tabs[action] = html
20 @html << html
21 end
22 alias_method :[]=, :add
23
24 def [](*args)
25 @tabs.values_at(*args)
26 end
27 end
28
29
30
31 module ProductsHelper
32 def subnav_links
33 t = TabHelper.new(self,
34 :active => Proc.new {|text, url| %|<div class="current tab">
35 :inactive => Proc.new {|text, url| %|<div class="tab">
36 )
37 t[:index] = 'Manage Products'
38 t[:front_page] = 'Manage Front Page'
39
40 '<div id="tabs">' +
41 '<div style="float: left">' +
42 t[:index, :front_page].join +
43 '</div>' +
44 '<div class="clear"></div>' +
45 '</div>'
46 end
47 end
class TabHelper
attr_reader :html, :tabs
def initialize(template, states)
@template = template
@states = states
@html = []
@tabs = {}
end
def add(action, text)
url = { :action => action }
html =
if @template.request.path.sub(/\?.*/, '') == @template.url_for(url)
@states[:active].call(text, url)
else
@states[:inactive].call(text, url)
end
@tabs[action] = html
@html << html
end
alias_method :[]=, :add
def [](*args)
@tabs.values_at(*args)
end
end
module ProductsHelper
def subnav_links
t = TabHelper.new(self,
:active => Proc.new {|text, url| %|<div class="current tab">
:inactive => Proc.new {|text, url| %|<div class="tab">
)
t[:index] = 'Manage Products'
t[:front_page] = 'Manage Front Page'
'<div id="tabs">' +
'<div style="float: left">' +
t[:index, :front_page].join +
'</div>' +
'<div class="clear"></div>' +
'</div>'
end
end
Send this to a friend
http://blog.obiefernandez.com/content/2008/06/railsconf-2008.html
1 def javascript_include_all
2 includes = ''
3 Dir.new("#{RAILS_ROOT}/public/javascripts").each do |js|
4 next if js == "." or js == '..' or !js[".js"]
5 includes += javascript_include_tag(js) + "\n"
6 end
7 end
def javascript_include_all
includes = ''
Dir.new("#{RAILS_ROOT}/public/javascripts").each do |js|
next if js == "." or js == '..' or !js[".js"]
includes += javascript_include_tag(js) + "\n"
end
end
Send this to a friend
this helper returns a current url
1 def current_url
2 url_for :only_path => false, :overwrite_params=>{}
3 end
def current_url
url_for :only_path => false, :overwrite_params=>{}
end
Send this to a friend
create an application helper to manage your rails messages
concept by nando vieira [simplesideias.com.br]
1
2 def flash_message
3 messages = ""
4 [:notice, :info, :warning, :error].each {|type|
5 if flash[type]
6 messages += "<p class=\"#{type}\" id=\"alert\">#{flash[type]}</p>"
7 end
8 }
9 messages
10 end
11
12
13
14
15 <%= flash_message %>
16
17 <% content_tag :script, :type => "text/javascript" do %>
18 $('alert').style.display = 'none';
19 new Effect.Appear('alert', {duration: 3});
20 setTimeout("new Effect.Fade('alert');", 10000);
21 <% end %>
def flash_message
messages = ""
[:notice, :info, :warning, :error].each {|type|
if flash[type]
messages += "<p class=\"#{type}\" id=\"alert\">#{flash[type]}</p>"
end
}
messages
end
<%= flash_message %>
<% content_tag :script, :type => "text/javascript" do %>
$('alert').style.display = 'none';
new Effect.Appear('alert', {duration: 3});
setTimeout("new Effect.Fade('alert');", 10000);
<% end %>