Skip to content

Commit

Permalink
initial commit of the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Jun 8, 2008
1 parent 598b968 commit 8b094c7
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 0 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Has a few controller and view helpers I use on most projects. Probably not beneficial for anyone else.
5 changes: 5 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'common_view_helpers'
require 'common_controller_helpers'

ActionController::Base.send :include, CommonControllerHelpers
ActionView::Base.send :include, CommonViewHelpers
17 changes: 17 additions & 0 deletions lib/.svn/all-wcprops
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
K 25
svn:wc:ra_dav:version-url
V 47
/public/!svn/ver/190/plugins/common_helpers/lib
END
common_controller_helpers.rb
K 25
svn:wc:ra_dav:version-url
V 76
/public/!svn/ver/190/plugins/common_helpers/lib/common_controller_helpers.rb
END
common_view_helpers.rb
K 25
svn:wc:ra_dav:version-url
V 70
/public/!svn/ver/190/plugins/common_helpers/lib/common_view_helpers.rb
END
52 changes: 52 additions & 0 deletions lib/.svn/entries
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
8

dir
190
http://svn.addictedtonew.com/public/plugins/common_helpers/lib
http://svn.addictedtonew.com/public



2008-04-06T05:33:15.254862Z
190
jnunemaker


svn:special svn:externals svn:needs-lock











fe7eae16-9a24-0410-a59d-9e59979e88be

common_controller_helpers.rb
file




2008-05-29T02:46:01.000000Z
1acd56cb0c6d487b14c301bc29860092
2008-04-06T05:33:15.254862Z
190
jnunemaker

common_view_helpers.rb
file




2008-05-29T02:46:01.000000Z
79299ec2125cbb6b379c3c4f46084f37
2008-04-06T05:33:15.254862Z
190
jnunemaker

1 change: 1 addition & 0 deletions lib/.svn/format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8
36 changes: 36 additions & 0 deletions lib/.svn/text-base/common_controller_helpers.rb.svn-base
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module CommonControllerHelpers
def self.included(base)
# also make this helper available to views
base.helper_method :get_all_gets
end

private
def return_to_or(default_url)
next_url = params[:return_to].blank? ? default_url : params[:return_to]
redirect_to next_url
end

def get_all_gets(options={})
options.reverse_merge!({ :pass => {}, :reject => [], :start_with => '?', :separator => '&' })

options[:reject] = options[:reject] << :controller << :action << :commit << :id

# add controller, action and commit to rejects, then flatten, uniq and to_s it
options[:reject].flatten.uniq
options[:reject].map!(&:to_s)

#raise options[:reject].inspect

# remove all params that are to be rejected
# the rejecting of params with key name including / is because of some change in edge rails
# that i have not investigated, but it works this way so i'm good for now
pass = params.delete_if { |k,v| options[:reject].include?(k.to_s) || k.to_s.include?('/') }.merge(options[:pass])

# convert non rejected parameters to a query string
qs = pass.inject(options[:start_with]) { |str,h| str += "#{h[0]}=#{h[1]}#{options[:separator]}"; str }
qs = qs.gsub(/^\?$/, '')
# remove trailing ampersand for cleaner url if there
qs = qs.gsub(/\&amp\;$/, '')
qs
end
end
102 changes: 102 additions & 0 deletions lib/.svn/text-base/common_view_helpers.rb.svn-base
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module CommonViewHelpers
def hidden_return_to_current(sep='?')
hidden_field_tag('return_to', request.request_uri)
end

def hidden_return_to_referer(sep='?')
r = request.env['HTTP_REFERER']
hidden_field_tag('return_to', r) unless r.blank?
end

# great for appending return_to parameter to form_for and link_to
# usage:
# link_to h(task.name), task_path(task) + return_to # => /tasks/1?return_to=/the/current/uri
def return_to(sep="?")
sep = sep == "&" ? "&amp;" : sep
"#{sep}return_to=#{CGI::escape(request.request_uri)}"
end

def label_tag(element_id, text, options={})
options.reverse_merge!({:for => element_id})
content_tag 'label', text, options
end

# Can be used with a string:
# <%- title 'Contacts' -%> # => sets title to Contacts, heading to Contacts
# <%- title "#{h(@contact.name)} // Contacts" -%> # => sets title to Contact Name // Contacts, heading to Contacts
#
# or with a string and a block:
# <%- title 'Contacts' do -%>
# <%= link_to 'Some Thing', some_path %>
# <%- end -%>
def title(page_title, &block)
content_for(:title) { page_title }
content_for(:heading) { block_given? ? capture(&block) : page_title.split(' // ').last }
end

def current_controller?(controller_name_or_array)
if controller_name_or_array.is_a?(Array)
return true if controller_name_or_array.include?(controller.controller_name)
else
return true if request.request_uri.starts_with?("/#{controller_name_or_array}") || controller.controller_name == controller_name_or_array
end
end

def current_action?(action_name_or_array)
is_array = action_name_or_array.is_a?(Array)

if ((is_array && action_name_or_array.include?(controller.action_name)) ||
(!is_array && controller.action_name == action_name_or_array))
true
else
false
end
end

def active_section?(controller_name_or_array, action_name_or_array=nil)
controller_name_or_array = controller_name_or_array.to_s
action_name_or_array = action_name_or_array.to_s if action_name_or_array
if action_name_or_array
controller.controller_name == controller_name_or_array && current_action?(action_name_or_array) ? true : false
else
current_controller?(controller_name_or_array) ? true : false
end
end

def active_class(controller_name_or_array, action_name_or_array=nil)
active_section?(controller_name_or_array, action_name_or_array) ? 'active' : ''
end

# for use on forms and such, creates a cancel link which will append query string
# variables and link to index
def link_to_cancel(*reject)
if params[:q].blank?
link_to 'cancel', url_for(:action => 'index') + get_all_gets(:reject => reject)
else
link_to 'cancel', url_for(:action => 'search') + get_all_gets(:reject => reject)
end
end

def link_to_view_all(options={})
options.reverse_merge!(:reject => [], :class => '')
css_class = options.delete(:class)
reject = options.delete(:reject)

if params[:q].blank?
link_to 'View All',
url_for(:action => 'index') + get_all_gets(:reject => reject),
:class => css_class
else
link_to 'Return To Search Results',
url_for(:action => 'search') + get_all_gets(:reject => reject),
:class => css_class
end
end

# displays flash messages by type of message
def display_flash_message
flash_types, string = [ :error, :warning, :notice ], ''
flash_types.each { |ft| string << "<div class=\"flash #{ft.to_s}\">#{flash[ft]}</div>" unless flash[ft].nil? }
string
end
end
36 changes: 36 additions & 0 deletions lib/common_controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module CommonControllerHelpers
def self.included(base)
# also make this helper available to views
base.helper_method :get_all_gets
end

private
def return_to_or(default_url)
next_url = params[:return_to].blank? ? default_url : params[:return_to]
redirect_to next_url
end

def get_all_gets(options={})
options.reverse_merge!({ :pass => {}, :reject => [], :start_with => '?', :separator => '&amp;' })

options[:reject] = options[:reject] << :controller << :action << :commit << :id

# add controller, action and commit to rejects, then flatten, uniq and to_s it
options[:reject].flatten.uniq
options[:reject].map!(&:to_s)

#raise options[:reject].inspect

# remove all params that are to be rejected
# the rejecting of params with key name including / is because of some change in edge rails
# that i have not investigated, but it works this way so i'm good for now
pass = params.delete_if { |k,v| options[:reject].include?(k.to_s) || k.to_s.include?('/') }.merge(options[:pass])

# convert non rejected parameters to a query string
qs = pass.inject(options[:start_with]) { |str,h| str += "#{h[0]}=#{h[1]}#{options[:separator]}"; str }
qs = qs.gsub(/^\?$/, '')
# remove trailing ampersand for cleaner url if there
qs = qs.gsub(/\&amp\;$/, '')
qs
end
end
102 changes: 102 additions & 0 deletions lib/common_view_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module CommonViewHelpers
def hidden_return_to_current(sep='?')
hidden_field_tag('return_to', request.request_uri)
end

def hidden_return_to_referer(sep='?')
r = request.env['HTTP_REFERER']
hidden_field_tag('return_to', r) unless r.blank?
end

# great for appending return_to parameter to form_for and link_to
# usage:
# link_to h(task.name), task_path(task) + return_to # => /tasks/1?return_to=/the/current/uri
def return_to(sep="?")
sep = sep == "&" ? "&amp;" : sep
"#{sep}return_to=#{CGI::escape(request.request_uri)}"
end

def label_tag(element_id, text, options={})
options.reverse_merge!({:for => element_id})
content_tag 'label', text, options
end

# Can be used with a string:
# <%- title 'Contacts' -%> # => sets title to Contacts, heading to Contacts
# <%- title "#{h(@contact.name)} // Contacts" -%> # => sets title to Contact Name // Contacts, heading to Contacts
#
# or with a string and a block:
# <%- title 'Contacts' do -%>
# <%= link_to 'Some Thing', some_path %>
# <%- end -%>
def title(page_title, &block)
content_for(:title) { page_title }
content_for(:heading) { block_given? ? capture(&block) : page_title.split(' // ').last }
end

def current_controller?(controller_name_or_array)
if controller_name_or_array.is_a?(Array)
return true if controller_name_or_array.include?(controller.controller_name)
else
return true if request.request_uri.starts_with?("/#{controller_name_or_array}") || controller.controller_name == controller_name_or_array
end
end

def current_action?(action_name_or_array)
is_array = action_name_or_array.is_a?(Array)

if ((is_array && action_name_or_array.include?(controller.action_name)) ||
(!is_array && controller.action_name == action_name_or_array))
true
else
false
end
end

def active_section?(controller_name_or_array, action_name_or_array=nil)
controller_name_or_array = controller_name_or_array.to_s
action_name_or_array = action_name_or_array.to_s if action_name_or_array
if action_name_or_array
controller.controller_name == controller_name_or_array && current_action?(action_name_or_array) ? true : false
else
current_controller?(controller_name_or_array) ? true : false
end
end

def active_class(controller_name_or_array, action_name_or_array=nil)
active_section?(controller_name_or_array, action_name_or_array) ? 'active' : ''
end

# for use on forms and such, creates a cancel link which will append query string
# variables and link to index
def link_to_cancel(*reject)
if params[:q].blank?
link_to 'cancel', url_for(:action => 'index') + get_all_gets(:reject => reject)
else
link_to 'cancel', url_for(:action => 'search') + get_all_gets(:reject => reject)
end
end

def link_to_view_all(options={})
options.reverse_merge!(:reject => [], :class => '')
css_class = options.delete(:class)
reject = options.delete(:reject)

if params[:q].blank?
link_to 'View All',
url_for(:action => 'index') + get_all_gets(:reject => reject),
:class => css_class
else
link_to 'Return To Search Results',
url_for(:action => 'search') + get_all_gets(:reject => reject),
:class => css_class
end
end

# displays flash messages by type of message
def display_flash_message
flash_types, string = [ :error, :warning, :notice ], ''
flash_types.each { |ft| string << "<div class=\"flash #{ft.to_s}\">#{flash[ft]}</div>" unless flash[ft].nil? }
string
end
end

0 comments on commit 8b094c7

Please sign in to comment.