diff --git a/app/controllers/statuses_controller.rb b/app/controllers/statuses_controller.rb index dbf4ad5..e2e793b 100644 --- a/app/controllers/statuses_controller.rb +++ b/app/controllers/statuses_controller.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 501bafc..a71d8f8 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -31,14 +31,11 @@
- <% flash.each do |key, value| -%> -
- <%= html_escape(value) %> -
- <% end -%> + <%= display_flash_message %>
<%- form_tag statuses_url, :method => :post do -%> + <%= hidden_return_to_current %> <%= hidden_field_tag 'in_reply_to_status_id' %> 140 diff --git a/app/views/layouts/login.html.erb b/app/views/layouts/login.html.erb index 7891fb1..e998898 100644 --- a/app/views/layouts/login.html.erb +++ b/app/views/layouts/login.html.erb @@ -16,12 +16,7 @@
- <% flash.each do |key, value| -%> -
- <%= html_escape(value) %> -
- <% end -%> - + <%= display_flash_message %> <%= yield %>
diff --git a/vendor/plugins/common_helpers/README b/vendor/plugins/common_helpers/README new file mode 100644 index 0000000..9aef755 --- /dev/null +++ b/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. \ No newline at end of file diff --git a/vendor/plugins/common_helpers/init.rb b/vendor/plugins/common_helpers/init.rb new file mode 100644 index 0000000..d684665 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/vendor/plugins/common_helpers/lib/common_controller_helpers.rb b/vendor/plugins/common_helpers/lib/common_controller_helpers.rb new file mode 100644 index 0000000..7ad2e80 --- /dev/null +++ b/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 => '&' }) + + 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(/\&\;$/, '') + qs + end +end \ No newline at end of file diff --git a/vendor/plugins/common_helpers/lib/common_view_helpers.rb b/vendor/plugins/common_helpers/lib/common_view_helpers.rb new file mode 100644 index 0000000..3c830f7 --- /dev/null +++ b/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 == "&" ? "&" : 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 << "
#{flash[ft]}
" unless flash[ft].nil? } + string + end +end \ No newline at end of file