Module: IconLinks::MethodMissing
- Includes:
- ViewHelpers
- Defined in:
- lib/icon_links/method_missing.rb
Instance Method Summary collapse
-
#method_missing_with_icon_links(method_id, *args) ⇒ Object
Handle missing methods so we can do fancy URL hooks.
Methods included from ViewHelpers
#collapsible_tag, #desc_of, #expandable_tag, #function_icon, #help_for, #help_icon, #help_text, #icon_for, #icon_tag, #icon_to, #icon_to_remote, #icon_url, #png_image_tag, #toggle_tag
Instance Method Details
#method_missing_with_icon_links(method_id, *args) ⇒ Object
Handle missing methods so we can do fancy URL hooks. This is what gives us “edit_player_icon” and “new_game_icon”
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/icon_links/method_missing.rb', line 7 def method_missing_with_icon_links(method_id, *args) method = method_id.to_s # special catch for "run_icon", where it really means "run_run_icon" method = "#{$1}_#{$1}_icon" if method =~ /^([a-z]+)_icon$/ if method =~ /^([a-z]+)_(.+)_icon$/ = args.last.is_a?(Hash) ? args.pop : {} if .has_key?(:if) return icon_tag(:clear) unless .delete(:if) end type = $1 meth = $2 icon = .delete(:icon) || type unless label = .delete(:label) label = meth.dup IconLinks.remove_prefixes_for_labels.each {|prefix| break if label.sub!(/^#{prefix}_/, '')} label = label.titleize end # Note: We pass *either* args OR options to a given xxx_path() method, # depending on whether it's a collection or member method. url = '' case type when 'new' url = send("new_#{meth}_path", ) #options[:rel] = "gb_page_center[600, 500]" # greybox [:title] ||= "Create a new #{label}" return icon_to(icon, url, ) when 'ajaxnew' url = send("new_#{meth}_path", ) #options[:rel] = "gb_page_center[600, 500]" # greybox [:title] ||= "Create a new #{label}" return icon_to(icon, url, ) when 'edit' url = send("edit_#{meth}_path", args) #options[:rel] = "gb_page_center[600, 500]" # greybox [:title] ||= "Edit this #{label}" return icon_to(icon, url, ) when 'delete' url = send("#{meth}_path", args) [:method] ||= :delete [:title] ||= "Delete this #{label}" return icon_to(icon, url, ) when 'ajaxdelete' # Delete a record with an id, ala user_path(user) # Fancy AJAX, so that it deletes the row in-place [:url] ||= send("#{meth}_path", args) [:method] ||= 'delete' show = .delete(:show) || args.first target = show.is_a?(ActiveRecord::Base) ? "#{show.class.name.to_s.underscore}_#{show.to_param}" : "#{show.to_param}" #options[:update] ||= target [:success] ||= "$('#{options[:update]}').hide;alert('success');" # I hate that sometimes in Rails, you need a fucking :html subelement htmlopt = {:title => "Delete this #{label}"} # If no condition, set a toggle so that we tell if we have clicked # on the button, and can collapse it appropriately. unless [:condition] [:id] = "#{options[:update]}_icon" end return link_to_remote(icon_tag(type, :id => [:id]), , htmlopt) when 'list' # Main index, this does NOT have an id, ie users_path # Fancy AJAX, so that it expands/collapses a sub-row = .dup .delete(:update) url = send("#{meth.pluralize}_path", ) show = .delete(:show) || .values.first target = show.is_a?(ActiveRecord::Base) ? "show_#{show.class.name.to_s.underscore}_#{show.to_param}" : "show_#{show.to_param}" [:update] ||= target [:url] ||= url [:method] ||= 'get' [:complete] ||= "$('#{options[:update]}').show();" # I hate that sometimes in Rails, you need a fucking :html subelement htmlopt = {:title => "List #{label.pluralize}"} # If no condition, set a toggle so that we tell if we have clicked # on the button, and can collapse it appropriately. extra_js = '' unless [:condition] [:id] = "#{options[:update]}_icon" var = "loaded_#{options[:update]}" [:before] = "#{var} = !#{var};" + " if (#{var}) { $('#{options[:update]}').hide();"+ " $('#{options[:id]}').src = '#{icon_url(:show)}'; return false }"+ " else { $('#{options[:id]}').src = '#{icon_url(:loading)}' }" [:complete] += "$('#{options[:id]}').src = '#{icon_url(:hide)}';" # Don't use javascript_tag or, ironically, Prototype chokes extra_js = '<script type="text/javascript">' + "#{var} = true;" + '</script>' end return extra_js + link_to_remote(icon_tag(:show, :id => [:id]), , htmlopt) when 'show' # Show a record with an id, ala user_path(user) # Fancy AJAX, so that it expands/collapses a sub-row [:url] ||= send("#{meth}_path", args) [:method] ||= 'get' show = .delete(:show) || args.first target = show.is_a?(ActiveRecord::Base) ? "show_#{show.class.name.to_s.underscore}_#{show.to_param}" : "show_#{show.to_param}" [:update] ||= target [:complete] ||= "$('#{options[:update]}').show();" # I hate that sometimes in Rails, you need a fucking :html subelement htmlopt = {:title => "Show more about this #{label}"} # If no condition, set a toggle so that we tell if we have clicked # on the button, and can collapse it appropriately. extra_js = '' unless [:condition] [:id] = "#{options[:update]}_icon" var = "loaded_#{options[:update]}" [:before] = "#{var} = !#{var};" + " if (#{var}) { $('#{options[:update]}').hide();"+ " $('#{options[:id]}').src = '#{icon_url(:show)}'; return false }"+ " else { $('#{options[:id]}').src = '#{icon_url(:loading)}' }" [:complete] += "$('#{options[:id]}').src = '#{icon_url(:hide)}';" # Don't use javascript_tag or, ironically, Prototype chokes extra_js = '<script type="text/javascript">' + "#{var} = true;" + '</script>' end return extra_js + link_to_remote(icon_tag(type, :id => [:id]), , htmlopt) when 'view' # Like "show", but we changed it to "view" to indicate new page # main index, this does NOT have an id url = send("#{meth}_path", args) [:title] ||= "View this #{label}" return icon_to(icon, url, ) else # This generic handler handles all other actions [:title] ||= "#{type.titleize} this #{label}" if [:url] url = .delete(:url) url[:controller] ||= meth.pluralize if url.is_a? Hash url[:id] ||= args[:id] if url.is_a? Hash and args.is_a? Hash elsif type == meth # call "run_path" for "run_run_icon" url = send("#{meth}_path", args) else # call "review_revision_path" for "review_revision_icon" url = send("#{type}_#{meth}_path", args) end if [:remote] htmlopt = {} htmlopt[:title] = .delete(:title) if .has_key?(:title) htmlopt[:id] = .delete(:id) if .has_key?(:id) [:url] = url return link_to_remote(icon_tag(icon), , htmlopt) else return icon_to(icon, url, ) end end end method_missing_without_icon_links(method_id, args) end |