js_info 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +96 -0
- data/Rakefile +1 -0
- data/js_info.gemspec +23 -0
- data/lib/js_info.rb +84 -0
- data/lib/js_info/version.rb +3 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chuyi Huang
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# JsInfo
|
2
|
+
|
3
|
+
JsInfo is a simple gem that trys to integrate javascript alert, confirm and prompt functions into Rails controller level. You can alert the users with a message without showing it in the view like the Flash of ActionDispatch does by using javascript functions.
|
4
|
+
|
5
|
+
The principles of JsInfo are:
|
6
|
+
|
7
|
+
1. Easy to use
|
8
|
+
2. Call from the controller
|
9
|
+
3. No extra codes in View layer
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'js_info'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install js_info
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
You can call JsInfo functions in the controllers:
|
28
|
+
|
29
|
+
js_alert("Your subscription is ok!")
|
30
|
+
js_confirm("Are you sure you want to visit google?", "http://www.google.com")
|
31
|
+
js_prompt("Please enter your name", "http://example.com")
|
32
|
+
|
33
|
+
|
34
|
+
### js_alert
|
35
|
+
|
36
|
+
And here is a typical example in the users controller:
|
37
|
+
|
38
|
+
def create
|
39
|
+
@user = User.find(params[:id])
|
40
|
+
if @user.save
|
41
|
+
js_alert("Your profile created successfully!")
|
42
|
+
else
|
43
|
+
render :new
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
You can specify the target url as the second parameter:
|
48
|
+
|
49
|
+
js_alert("We are moving to github!", "https://github.com")
|
50
|
+
|
51
|
+
|
52
|
+
or more conventional way:
|
53
|
+
|
54
|
+
js_alert("Welcome Back!", root_path)
|
55
|
+
|
56
|
+
|
57
|
+
Please note that the default of the target url is *request.referer*
|
58
|
+
|
59
|
+
### js_confirm
|
60
|
+
|
61
|
+
Sometimes a yes-no question is required:
|
62
|
+
|
63
|
+
js_confirm("Are you sure you want to sign up?", new_subscription_path)
|
64
|
+
|
65
|
+
As for js_confirm, the second parameter is necessary. If you want to redirect to another path when the user click **no** or **cancel**, it is possible to use the third parameter:
|
66
|
+
|
67
|
+
js_confirm("Are you sure you want sign up?", new_subscription_path, cancel_subscription_path)
|
68
|
+
|
69
|
+
Also note that the default value for third parameter is *request.referer*, so when the user click **cancel** will return to the previous page.
|
70
|
+
|
71
|
+
### js_prompt
|
72
|
+
|
73
|
+
js_prompt is a method that will carry user's input to developer's target url with specified parameter name, here is an example:
|
74
|
+
|
75
|
+
js_prompt("what is your name?", new_user_path)
|
76
|
+
|
77
|
+
|
78
|
+
The typical javascript prompt will pop up with a dialog as you expected. As you can key in some values in the input box. When the user press the **Enter**, it will redirect to the new_user_path with a parameter named *params_name* with the value of user's input, say, *Chuyi* is the value of the input. it might look like **"http://example.com?params_name=Chuyi"** in the location bar.
|
79
|
+
|
80
|
+
Of course we can define the default value of the input or change the parameter to fit our needs:
|
81
|
+
|
82
|
+
js_prompt("Enter your email", new_user_path, "email", "yourname@example.com")
|
83
|
+
|
84
|
+
After pressing the confirm button the page will be redirected to **"http://example.com?email=yourname@example.com"**. As you can see, the third parameter is the parameter name and the forth one is the default value, simple as that.
|
85
|
+
|
86
|
+
##What you need to know
|
87
|
+
|
88
|
+
I test it in the rails development environment, so please let me know if there is any improvement or bug I need to fix, thank you.
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
1. Fork it
|
93
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
94
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
95
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
96
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/js_info.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'js_info/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "js_info"
|
8
|
+
gem.version = JsInfo::VERSION
|
9
|
+
gem.authors = ["Chuyi Huang"]
|
10
|
+
gem.email = ["chuyihuang@gmail.com"]
|
11
|
+
gem.description = %q{Make javascript alert, confirm and prompt as controller level methods}
|
12
|
+
gem.summary = %q{Instead of using flash as the alert to the users, you can replace it with js_info which is simplier and code free in the view layers}
|
13
|
+
gem.homepage = "https://github.com/chuyihuang/js_info"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
#require rails
|
21
|
+
gem.add_dependency "actionpack", ">= 3.0.0"
|
22
|
+
|
23
|
+
end
|
data/lib/js_info.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require "js_info/version"
|
2
|
+
|
3
|
+
module JsInfo
|
4
|
+
|
5
|
+
protected
|
6
|
+
|
7
|
+
def js_alert(message, url = request.referer)
|
8
|
+
if message.blank?
|
9
|
+
raise "Unable to find any message for js_info!"
|
10
|
+
end
|
11
|
+
|
12
|
+
script = %Q{alert("#{message}");document.location.href="#{url}";}
|
13
|
+
html = html(script)
|
14
|
+
render text: html
|
15
|
+
return
|
16
|
+
end
|
17
|
+
|
18
|
+
def js_confirm(message, url, false_url = nil)
|
19
|
+
if message.blank? || url.blank?
|
20
|
+
raise "wrong parameter values for js_info!"
|
21
|
+
end
|
22
|
+
|
23
|
+
false_action = false_url.blank? ? "document.location.href=\"#{request.referer}\"" : "document.location.href=\"#{false_url}\""
|
24
|
+
|
25
|
+
script = %Q{
|
26
|
+
var r = confirm("#{message}");
|
27
|
+
if (r == true) {
|
28
|
+
document.location.href="#{url}";
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
#{false_action}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
html = html(script)
|
35
|
+
render text: html
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
def js_prompt(message, url, params_name = "prompt_name", params_value = nil)
|
40
|
+
|
41
|
+
if message.blank? || url.blank?
|
42
|
+
raise "wrong parameter values for js_info!"
|
43
|
+
end
|
44
|
+
|
45
|
+
script = %Q{
|
46
|
+
var url = "#{url}";
|
47
|
+
var params_name = "#{params_name}";
|
48
|
+
var params_value = prompt("#{message}", "#{params_value}");
|
49
|
+
var target_url = url + "?" + params_name + "=" + params_value;
|
50
|
+
//check is params_value is existed
|
51
|
+
if (params_value !== null) {
|
52
|
+
document.location.href=target_url;
|
53
|
+
}
|
54
|
+
else {
|
55
|
+
document.location.href="#{request.referer}";
|
56
|
+
}
|
57
|
+
}
|
58
|
+
html = html(script)
|
59
|
+
render text: html
|
60
|
+
return
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def html(script)
|
66
|
+
%Q{
|
67
|
+
<!DOCTYPE html>
|
68
|
+
<head></head>
|
69
|
+
<body>
|
70
|
+
<script type="text/javascript">
|
71
|
+
<!--
|
72
|
+
#{script}
|
73
|
+
//-->
|
74
|
+
</script>
|
75
|
+
</body>
|
76
|
+
</html>
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
class ActionController::Base
|
83
|
+
include JsInfo
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: js_info
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chuyi Huang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
description: Make javascript alert, confirm and prompt as controller level methods
|
31
|
+
email:
|
32
|
+
- chuyihuang@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- js_info.gemspec
|
43
|
+
- lib/js_info.rb
|
44
|
+
- lib/js_info/version.rb
|
45
|
+
homepage: https://github.com/chuyihuang/js_info
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.23
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Instead of using flash as the alert to the users, you can replace it with
|
69
|
+
js_info which is simplier and code free in the view layers
|
70
|
+
test_files: []
|