Skip to content

Commit

Permalink
Now returning to current page when creating a new tweet instead of al…
Browse files Browse the repository at this point in the history
…ways going to root url.
  • Loading branch information
jnunemaker committed Apr 9, 2009
1 parent a7a9ed6 commit 270021c
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/statuses_controller.rb
Expand Up @@ -25,6 +25,6 @@ def create

tweet = current_user.client.update(params[:text], options)
flash[:notice] = "Got it! Tweet ##{tweet.id} created."
redirect_to root_url
return_to_or root_url
end
end
7 changes: 2 additions & 5 deletions app/views/layouts/application.html.erb
Expand Up @@ -31,14 +31,11 @@

<div id="content">
<div id="content_inner">
<% flash.each do |key, value| -%>
<div class="flash <%= key %>">
<%= html_escape(value) %>
</div>
<% end -%>
<%= display_flash_message %>

<div id="update">
<%- form_tag statuses_url, :method => :post do -%>
<%= hidden_return_to_current %>
<%= hidden_field_tag 'in_reply_to_status_id' %>
<span id="countdown">140</span>
<label for="text">Drop a new tweet like it's hot!</label>
Expand Down
7 changes: 1 addition & 6 deletions app/views/layouts/login.html.erb
Expand Up @@ -16,12 +16,7 @@

<div id="content">
<div id="content_inner">
<% flash.each do |key, value| -%>
<div class="flash <%= key %>">
<%= html_escape(value) %>
</div>
<% end -%>
<%= display_flash_message %>
<%= yield %>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions vendor/plugins/common_helpers/README
@@ -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 vendor/plugins/common_helpers/init.rb
@@ -0,0 +1,5 @@
require 'common_view_helpers'
require 'common_controller_helpers'

ActionController::Base.send :include, CommonControllerHelpers
ActionView::Base.send :include, CommonViewHelpers
36 changes: 36 additions & 0 deletions vendor/plugins/common_helpers/lib/common_controller_helpers.rb
@@ -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
106 changes: 106 additions & 0 deletions vendor/plugins/common_helpers/lib/common_view_helpers.rb
@@ -0,0 +1,106 @@
module CommonViewHelpers
def hidden_return_to_current(sep='?')
content_tag(:div, hidden_field_tag('return_to', request.request_uri))
end

def hidden_return_to_referer(sep='?')
r = request.env['HTTP_REFERER']
content_tag(:div, 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 url_for_return_to(args)
params[:return_to].blank? ? url_for(args) : params[:return_to]
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 270021c

Please sign in to comment.