image_button_to 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +111 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/lib/image_button_to.rb +122 -0
- data/test/helper.rb +10 -0
- data/test/test_image_button_to.rb +7 -0
- metadata +74 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 José Pablo Fernández Silva
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
= Image Button To
|
2
|
+
|
3
|
+
Ruby on Rails provides the following helpers:
|
4
|
+
|
5
|
+
* +button_to+
|
6
|
+
|
7
|
+
* +button_to_function+
|
8
|
+
|
9
|
+
* +button_to_remote+
|
10
|
+
|
11
|
+
which wrap a form around a +submit_tag+ to easily create buttons pointing to
|
12
|
+
some action.
|
13
|
+
|
14
|
+
What Ruby on Rails doesn't provide is a counterpart for +image_submit_tag+. This
|
15
|
+
gem provides that. It basically contains three helpers:
|
16
|
+
|
17
|
+
* +image_button_to+
|
18
|
+
|
19
|
+
* +image_button_to_remote+
|
20
|
+
|
21
|
+
* +image_button_to_function+
|
22
|
+
|
23
|
+
== Installation
|
24
|
+
|
25
|
+
This gem is provided through Gemcutter so you need to have gem configured to
|
26
|
+
pull gems from Gemcutter.
|
27
|
+
|
28
|
+
=== Enabling Gemcutter
|
29
|
+
|
30
|
+
A properly configured environment would be like this:
|
31
|
+
|
32
|
+
$ gem sources
|
33
|
+
*** CURRENT SOURCES ***
|
34
|
+
|
35
|
+
http://gemcutter.org
|
36
|
+
http://gems.rubyforge.org/
|
37
|
+
http://gems.github.com
|
38
|
+
|
39
|
+
If you don't have http://gemcutter.org in your sources then you need to add. I
|
40
|
+
know two ways to do. One is installing Gemcutter and running gem tumble:
|
41
|
+
|
42
|
+
$ sudo gem install gemcutter
|
43
|
+
$ gem tumble
|
44
|
+
|
45
|
+
Be careful that gem tumble will remove Gemcutter from your repositories if it's
|
46
|
+
already there.
|
47
|
+
|
48
|
+
The other way is by hand like this:
|
49
|
+
|
50
|
+
$ gem source -a http://gemcutter.org
|
51
|
+
|
52
|
+
I'm not sure if there's any difference. I think there isn't one.
|
53
|
+
|
54
|
+
=== Installing image_button_to by hand
|
55
|
+
|
56
|
+
It's simple a matter of running:
|
57
|
+
|
58
|
+
$ gem install image_button_to
|
59
|
+
|
60
|
+
and that's it. Let me know if something breaks.
|
61
|
+
|
62
|
+
=== Installing through your Ruby on Rails project
|
63
|
+
|
64
|
+
In the +environment.rb+ file of your Ruby on Rails project you'll have some
|
65
|
+
commented out lines like this:
|
66
|
+
|
67
|
+
# config.gem "bj"
|
68
|
+
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
|
69
|
+
# config.gem "sqlite3-ruby", :lib => "sqlite3"
|
70
|
+
# config.gem "aws-s3", :lib => "aws/s3"
|
71
|
+
|
72
|
+
After those you can just add
|
73
|
+
|
74
|
+
config.gem "image_button_to"
|
75
|
+
|
76
|
+
and then run
|
77
|
+
|
78
|
+
$ rake gems:install
|
79
|
+
|
80
|
+
and you'll get this and all the gems your Rails project need installed.
|
81
|
+
Configuring your Rails project like that is something you'll need anyway, so
|
82
|
+
this is my recommended way.
|
83
|
+
|
84
|
+
== Using the helpers
|
85
|
+
|
86
|
+
Be sure to have you Rails project properly configured to load this gem like it's
|
87
|
+
explained in the previous section, in "Installing through your Ruby on Rails
|
88
|
+
project". After that, using each of the methods is equivalent to using the
|
89
|
+
non-image provided by Ruby on Rails, but passing the path to the image instead
|
90
|
+
of the text.
|
91
|
+
|
92
|
+
The path to the image is treated in the same way image_submit_tags treat it
|
93
|
+
(appending "images/" and whatnot). Some examples from
|
94
|
+
http://isitsciencefiction.com (where this code was born):
|
95
|
+
|
96
|
+
<%= image_button_to "yes-up.png", vote_up_item_url(:id => @item.id) %>
|
97
|
+
<%= image_button_to "no-up.png", vote_down_item_url(:id => @item.id) %>
|
98
|
+
<%= image_button_to_remote "yes-up-small.png", :update => item.slug, :url => vote_up_item_url(:id => item.id) %>
|
99
|
+
<%= image_button_to_remote "no-up-small.png", :update => item.slug, :url => vote_down_item_url(:id => item.id) %>
|
100
|
+
|
101
|
+
Check the documentation for image_button_tag and button_to, this is just a
|
102
|
+
merge.
|
103
|
+
|
104
|
+
== Documentation
|
105
|
+
|
106
|
+
Up to date documentation should be automatically generated on
|
107
|
+
http://rdoc.info/projects/pupeno/image_button_to
|
108
|
+
|
109
|
+
== Copyright
|
110
|
+
|
111
|
+
Copyright (c) 2009 José Pablo Fernández Silva. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Copyright (c) 2009 José Pablo Fernández Silva. See LICENSE for details.
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "image_button_to"
|
10
|
+
gem.summary = "Image equivalent to Rails button_to"
|
11
|
+
gem.description = "Ruby on Rails provides a series of methods called button_to and image_submit_tag, but no image_button_to. This gem provides that."
|
12
|
+
gem.email = "pupeno@pupeno.com"
|
13
|
+
gem.homepage = "http://github.com/pupeno/image_button_to"
|
14
|
+
gem.authors = ["J. Pablo Fernández"]
|
15
|
+
#gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
16
|
+
gem.add_dependency "actionpack", ">= 2.0.0"
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
Rake::TestTask.new(:test) do |test|
|
26
|
+
test.libs << 'lib' << 'test'
|
27
|
+
test.pattern = 'test/**/test_*.rb'
|
28
|
+
test.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
begin
|
32
|
+
require 'rcov/rcovtask'
|
33
|
+
Rcov::RcovTask.new do |test|
|
34
|
+
test.libs << 'test'
|
35
|
+
test.pattern = 'test/**/test_*.rb'
|
36
|
+
test.verbose = true
|
37
|
+
end
|
38
|
+
rescue LoadError
|
39
|
+
task :rcov do
|
40
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
task :test => :check_dependencies
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/rdoctask'
|
49
|
+
Rake::RDocTask.new do |rdoc|
|
50
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "Image Button To #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('LICENSE')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
rdoc.options << '--charset' << 'utf-8'
|
58
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# Copyright (c) 2009 José Pablo Fernández Silva. See LICENSE for details.
|
2
|
+
|
3
|
+
module ImageButtonTo
|
4
|
+
|
5
|
+
# Generates a form containing a single image button that submits to the URL
|
6
|
+
# created by the set of +options+. This is the safest method to ensure links
|
7
|
+
# that cause changes to your data are not triggered by search bots or
|
8
|
+
# accelerators.
|
9
|
+
#
|
10
|
+
# The generated form element has a class name of <tt>image-button-to</tt> to
|
11
|
+
# allow styling of the form itself and its children. You can control the
|
12
|
+
# form submission and input element behavior using +html_options+. This
|
13
|
+
# method accepts the <tt>:method</tt> and <tt>:confirm</tt> modifiers
|
14
|
+
# described in the +link_to+ documentation. If no <tt>:method</tt> modifier
|
15
|
+
# is given, it will default to performing a POST operation. You can also
|
16
|
+
# disable the button by passing <tt>:disabled => true</tt> in
|
17
|
+
# +html_options+. If you are using RESTful routes, you can pass the
|
18
|
+
# <tt>:method</tt> to change the HTTP verb used to submit the form.
|
19
|
+
#
|
20
|
+
# ==== Options
|
21
|
+
# The +options+ hash accepts the same options as url_for.
|
22
|
+
#
|
23
|
+
# There are a few special +html_options+:
|
24
|
+
# * <tt>:method</tt> - Specifies the anchor name to be appended to the path.
|
25
|
+
# * <tt>:disabled</tt> - Specifies the anchor name to be appended to the
|
26
|
+
# path.
|
27
|
+
# * <tt>:confirm</tt> - This will add a JavaScript confirm prompt with the
|
28
|
+
# question specified. If the user accepts, the link is processed normally,
|
29
|
+
# otherwise no action is taken.
|
30
|
+
#
|
31
|
+
# ==== Examples
|
32
|
+
# <%= button_to "new.png", :action => "new" %>
|
33
|
+
# # => "<form method="post" action="/controller/new" class="image-button-to">
|
34
|
+
# # <div><input src="/images/new.png?1257839010" type="image" /></div>
|
35
|
+
# # </form>"
|
36
|
+
#
|
37
|
+
# button_to "delete.png", { :action => "delete", :id => @image.id },
|
38
|
+
# :confirm => "Are you sure?", :method => :delete
|
39
|
+
# # => "<form method="post" action="/images/delete/1" class="image-button-to">
|
40
|
+
# # <div>
|
41
|
+
# # <input type="hidden" name="_method" value="delete" />
|
42
|
+
# # <input onclick="return confirm('Are you sure?');"
|
43
|
+
# # src="/images/new.png?1257839010" type="image" />
|
44
|
+
# # </div>
|
45
|
+
# # </form>"
|
46
|
+
def image_button_to(source, options = {}, html_options = {})
|
47
|
+
html_options = html_options.stringify_keys
|
48
|
+
convert_boolean_attributes!(html_options, %w( disabled ))
|
49
|
+
|
50
|
+
method_tag = ''
|
51
|
+
if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
|
52
|
+
method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
|
53
|
+
end
|
54
|
+
|
55
|
+
form_method = method.to_s == 'get' ? 'get' : 'post'
|
56
|
+
|
57
|
+
request_token_tag = ''
|
58
|
+
if form_method == 'post' && protect_against_forgery?
|
59
|
+
request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
|
60
|
+
end
|
61
|
+
|
62
|
+
if confirm = html_options.delete("confirm")
|
63
|
+
html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
|
64
|
+
end
|
65
|
+
|
66
|
+
url = options.is_a?(String) ? options : self.url_for(options)
|
67
|
+
|
68
|
+
"<form method=\"#{form_method}\" action=\"#{escape_once url}\" class=\"image-button-to\"><div>" +
|
69
|
+
method_tag + image_submit_tag(source, html_options) + request_token_tag + "</div></form>"
|
70
|
+
end
|
71
|
+
|
72
|
+
# Creates an image button with an onclick event which calls a remote action
|
73
|
+
# via XMLHttpRequest
|
74
|
+
#
|
75
|
+
# The options for specifying the target with :url and defining
|
76
|
+
# callbacks is the same as link_to_remote except that +source+ is the path
|
77
|
+
# for an image.
|
78
|
+
def image_button_to_remote(source, options = {}, html_options = {})
|
79
|
+
image_button_to_function(source, remote_function(options), html_options)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns an image button with the given +name+ text that'll trigger a
|
83
|
+
# JavaScript +function+ using the onclick handler.
|
84
|
+
#
|
85
|
+
# The first argument +source+ is the filename of the button as treated by
|
86
|
+
# +image_submit_tag+, that is, passed to AssetTagHelper#image_path.
|
87
|
+
#
|
88
|
+
# The next arguments are optional and may include the javascript function
|
89
|
+
# definition and a hash of html_options.
|
90
|
+
#
|
91
|
+
# The +function+ argument can be omitted in favor of an +update_page+ block,
|
92
|
+
# which evaluates to a string when the template is rendered (instead of
|
93
|
+
# making an Ajax request first).
|
94
|
+
#
|
95
|
+
# The +html_options+ will accept a hash of html attributes for the link tag.
|
96
|
+
# Some examples are :class => "nav_button", :id => "articles_nav_button"
|
97
|
+
#
|
98
|
+
# Note: if you choose to specify the javascript function in a block, but
|
99
|
+
# would like to pass html_options, set the +function+ parameter to nil
|
100
|
+
#
|
101
|
+
# Examples:
|
102
|
+
# button_to_function "greeting.png", "alert('Hello world!')"
|
103
|
+
# button_to_function "delete.png", "if (confirm('Really?')) do_delete()"
|
104
|
+
# button_to_function "details.png" do |page|
|
105
|
+
# page[:details].visual_effect :toggle_slide
|
106
|
+
# end
|
107
|
+
# button_to_function "details.png", :class => "details_button" do |page|
|
108
|
+
# page[:details].visual_effect :toggle_slide
|
109
|
+
# end
|
110
|
+
def image_button_to_function(source, *args, &block)
|
111
|
+
html_options = args.extract_options!.symbolize_keys
|
112
|
+
|
113
|
+
function = block_given? ? update_page(&block) : args[0] || ''
|
114
|
+
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};"
|
115
|
+
|
116
|
+
image_submit_tag(source, html_options.merge(:onclick => onclick))
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
ActionController::Base.class_eval do
|
121
|
+
helper ImageButtonTo
|
122
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image_button_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "J. Pablo Fern\xC3\xA1ndez"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-24 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.0
|
24
|
+
version:
|
25
|
+
description: Ruby on Rails provides a series of methods called button_to and image_submit_tag, but no image_button_to. This gem provides that.
|
26
|
+
email: pupeno@pupeno.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/image_button_to.rb
|
42
|
+
- test/helper.rb
|
43
|
+
- test/test_image_button_to.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/pupeno/image_button_to
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Image equivalent to Rails button_to
|
72
|
+
test_files:
|
73
|
+
- test/helper.rb
|
74
|
+
- test/test_image_button_to.rb
|