rails_responsive_rater 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +106 -0
- data/Rakefile +2 -0
- data/lib/generators/rails_responsive_rater/install/install_generator.rb +26 -0
- data/lib/rails_responsive_rater/version.rb +3 -0
- data/lib/rails_responsive_rater/view_helpers/action_view.rb +14 -0
- data/lib/rails_responsive_rater.rb +18 -0
- data/rails_responsive_rater.gemspec +17 -0
- data/vendor/assets/javascripts/jquery.responsive-rater.js +152 -0
- data/vendor/assets/stylesheets/jquery.responsive-rater.css.sass +23 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 swalke16
|
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,106 @@
|
|
1
|
+
# Rails Responsive Rater
|
2
|
+
|
3
|
+
A simple client-side rating system packaged up as a gem for Rails projects.
|
4
|
+
|
5
|
+
This project was created because I was not happy with any of the existing rating plugins
|
6
|
+
I found for Rails. Mainly because they all relied on images (usually stars) for the rating
|
7
|
+
display. I think a better approach is to make use of unicode symbols and CSS so that the
|
8
|
+
rating display can easily be made responsive without the associated hassles of using images.
|
9
|
+
|
10
|
+
### What It Does
|
11
|
+
|
12
|
+
* Provides a responsive display of a rating (default stars) that is easily styleable.
|
13
|
+
* Provides a client-side API for interacting with the rating.
|
14
|
+
* Provides a Rails view helper for easily creating the client-side rating display.
|
15
|
+
|
16
|
+
### What It Does *NOT* Do
|
17
|
+
|
18
|
+
* Touch your Rails models, controllers, routes, etc... in any way.
|
19
|
+
* Interact with your Rails app via AJAX in any way.
|
20
|
+
* Work in older browsers...? This has only been tested in newer browsers. If you want it to work in older browsers feel free to fork and submit a pull request.
|
21
|
+
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
gem 'rails_responsive_rater'
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install rails_responsive_rater
|
36
|
+
|
37
|
+
|
38
|
+
Once the gem has been installed you can attempt to have it add the necessary require statements to your application.js and application.css for you:
|
39
|
+
|
40
|
+
$ rails generate rails_responsive_rater:install
|
41
|
+
|
42
|
+
If the generator does not work then manually require the JS and CSS assets in your manifest files by:
|
43
|
+
|
44
|
+
//= require jquery.responsive-rater
|
45
|
+
|
46
|
+
*= require jquery.responsive-rater
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
In your view you can use the view helper to easily create a rater with the default options:
|
51
|
+
|
52
|
+
<%= responsive_rater_tag() %>
|
53
|
+
|
54
|
+
### Options
|
55
|
+
|
56
|
+
The helper can take a hash of options to override the defaults and customize the behavior:
|
57
|
+
|
58
|
+
* field - (null) a jQuery selector that identifies a text, select, or number HTML form element that will be used to get the initial rating value from. The value will be kept in sync with the displayed rating as the user changes it.
|
59
|
+
* min - (0) minimum rating value.
|
60
|
+
* max - (5) maximum rating value, also corresponds to how many rating items to display.
|
61
|
+
* readonly - (false) control whether the user can change the displayed rating.
|
62
|
+
* value - (min) the value of the rating to start with.
|
63
|
+
* content - (★) the content to use for the rating display. This can be any valid HTML. Let your imagination go wild!
|
64
|
+
|
65
|
+
### Client-side API
|
66
|
+
|
67
|
+
This is written as a jQuery plugin and its API functions as other jQuery plugins do.
|
68
|
+
|
69
|
+
// construct a new rater inside a container, and provide some options
|
70
|
+
// available options are the same described above for the view helper, passed as an object literal
|
71
|
+
$(".rating-container").responsiveRater(options)
|
72
|
+
|
73
|
+
// get / set the value of the rater
|
74
|
+
$(".rating-container").responsiveRater("value")
|
75
|
+
$(".rating-container").responsiveRater("value", newValue)
|
76
|
+
|
77
|
+
// get / set readonly mode
|
78
|
+
$(".rating-container").responsiveRater("readonly")
|
79
|
+
$(".rating-container").responsiveRater("readonly", newValue)
|
80
|
+
|
81
|
+
// listen to an event when the rating changes
|
82
|
+
$(".rating-container").on("rated", function(value){
|
83
|
+
// do something with new value here
|
84
|
+
});
|
85
|
+
|
86
|
+
// listen to an event when the rating is reset
|
87
|
+
// reset occurs when a user clicks the highest previously rated value, e.g. clicking the 3rd star after giving a rating of 3
|
88
|
+
$(".rating-container").on("reset", function(value){
|
89
|
+
// do something with new value here
|
90
|
+
});
|
91
|
+
|
92
|
+
### Example Page
|
93
|
+
See the <a href="http://swalke16.github.io/rails_responsive_rater" target="_blank">example page</a> to see what the rater looks and functions like.
|
94
|
+
|
95
|
+
## Credits
|
96
|
+
[Derek Briggs](https://github.com/derekbriggs) - for the idea to go the responsive route in the first place, making the styles work, creating an example page, and generally being awesome.
|
97
|
+
|
98
|
+
[rails-rateit](https://github.com/ouvrages/rails-rateit) - for the initial idea and some code for working around edge cases.
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
1. Fork it
|
103
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
104
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
105
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
106
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module RailsResponsiveRater
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < ::Rails::Generators::Base
|
6
|
+
|
7
|
+
desc "This generator installs the Rails Responsive Rater plugin"
|
8
|
+
|
9
|
+
def inject_javascript
|
10
|
+
begin
|
11
|
+
inject_into_file "app/assets/javascripts/application.js", "//= require jquery.responsive-rater\n", :after => "//= require jquery\n"
|
12
|
+
rescue
|
13
|
+
puts "Could not automatically add jquery.responsive-rater script require into application.js! Please manually\n\n //= require jquery.responsive-rater"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def inject_stylesheet
|
18
|
+
begin
|
19
|
+
inject_into_file "app/assets/stylesheets/application.css", "*= require jquery.responsive-rater\n", :after => "/*\n"
|
20
|
+
rescue
|
21
|
+
puts "Could not automatically add jquery.responsive-rater stylesheet require into application.css! Please manually\n\n *= require jquery.responsive-rater"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module RailsResponsiveRater
|
2
|
+
module ViewHelpers
|
3
|
+
module ActionView
|
4
|
+
def responsive_rater_tag(options = {})
|
5
|
+
tag_options = {:class => "responsive-rater"}
|
6
|
+
options.each do |name, value|
|
7
|
+
tag_options["data-rater-#{name}"] = value
|
8
|
+
end
|
9
|
+
|
10
|
+
content_tag(:div, "", tag_options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rails_responsive_rater/version"
|
2
|
+
|
3
|
+
module RailsResponsiveRater
|
4
|
+
module Rails
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Railtie < ::Rails::Railtie
|
10
|
+
initializer "rails_responsive_rater.action_view" do |app|
|
11
|
+
ActiveSupport.on_load :action_view do
|
12
|
+
require 'rails_responsive_rater/view_helpers/action_view'
|
13
|
+
include RailsResponsiveRater::ViewHelpers::ActionView
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rails_responsive_rater/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Scott Walker", "Derek Briggs"]
|
6
|
+
gem.email = ["swalke16@gmail.com", "derek.briggs@me.com"]
|
7
|
+
gem.description = %q{This project was created because I was not happy with any of the existing rating plugins I found for Rails. Mainly because they all relied on images (usually stars) for the rating display. I think a better approach is to make use of unicode symbols and CSS so that the rating display can easily be made responsive without the associated hassles of using images.}
|
8
|
+
gem.summary = %q{A simple, responsive, client-side rating system Rails.}
|
9
|
+
gem.homepage = "https://github.com/swalke16/rails_responsive_rater"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rails_responsive_rater"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RailsResponsiveRater::VERSION
|
17
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
(function($) {
|
2
|
+
|
3
|
+
var methods = {
|
4
|
+
init : function(options) {
|
5
|
+
options = $.extend({}, $.fn.responsiveRater.defaults, options);
|
6
|
+
|
7
|
+
var $item = $(this);
|
8
|
+
|
9
|
+
if (!$item.data('rater-init')) {
|
10
|
+
|
11
|
+
if (!$item.hasClass('responsive-rater')) $item.addClass('responsive-rater');
|
12
|
+
|
13
|
+
//get our values, either from the data-* html5 attribute or from the options.
|
14
|
+
$item.data('rater-min', $item.data('rater-min') || options.min)
|
15
|
+
$item.data('rater-max', $item.data('rater-max') || options.max)
|
16
|
+
$item.data('rater-readonly', $item.data('rater-readonly') !== undefined ? $item.data("rater-readonly") : options.readonly)
|
17
|
+
$item.data('rater-field', $item.data('rater-field') || options.field)
|
18
|
+
$item.data('rater-value', $item.data('rater-value') || options.min)
|
19
|
+
$item.data('rater-content', $item.data('rater-content') || options.content)
|
20
|
+
|
21
|
+
|
22
|
+
var field = $item.data("rater-field");
|
23
|
+
if (field) {
|
24
|
+
field = $(field);
|
25
|
+
$item.data('rater-value', field.hide().val());
|
26
|
+
|
27
|
+
if (field[0].nodeName == 'INPUT') {
|
28
|
+
if (field[0].type == 'range' || field[0].type == 'text') { //in browsers not support the range type, it defaults to text
|
29
|
+
|
30
|
+
$item.data('rater-min', parseInt(field.attr('min')) || $item.data('rater-min')); //if we would have done fld[0].min it wouldn't have worked in browsers not supporting the range type.
|
31
|
+
$item.data('rater-max', parseInt(field.attr('max')) || $item.data('rater-max'));
|
32
|
+
}
|
33
|
+
}
|
34
|
+
if (field[0].nodeName == 'SELECT' && field[0].options.length > 1) {
|
35
|
+
$item.data('rater-min', Number(field[0].options[0].value));
|
36
|
+
$item.data('rater-max', Number(field[0].options[field[0].length - 1].value));
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
//Create the necessary tags.
|
41
|
+
for(var i=0; i<$item.data('rater-max'); i++){
|
42
|
+
//TODO: font size needs to scale with # of stars
|
43
|
+
$item.append('<span class="rating" style="width: ' + (100 / $item.data("rater-max")) + '%;">' + $item.data("rater-content") + '</span>');
|
44
|
+
}
|
45
|
+
|
46
|
+
$item.data('rater-init', true);
|
47
|
+
}
|
48
|
+
|
49
|
+
//set the value if we have it.
|
50
|
+
if ($item.data('rater-value')) {
|
51
|
+
var score = $item.data('rater-value');
|
52
|
+
$item.find('.rating:lt(' + score + ')').addClass("rated");
|
53
|
+
}
|
54
|
+
|
55
|
+
methods.bindEvents.call(this);
|
56
|
+
},
|
57
|
+
|
58
|
+
bindEvents : function() {
|
59
|
+
var $item = $(this);
|
60
|
+
|
61
|
+
if (!$item.data('rater-readonly')) {
|
62
|
+
|
63
|
+
if (!$item.data('rater-bound')) {
|
64
|
+
$item.on("mouseenter mouseleave", ".rating", function(){
|
65
|
+
$(this).toggleClass('hover').prevAll().toggleClass('hover');
|
66
|
+
});
|
67
|
+
|
68
|
+
$item.on("click", ".rating", function(e) {
|
69
|
+
e.preventDefault();
|
70
|
+
var newValue, event;
|
71
|
+
|
72
|
+
if( $(this).hasClass('rated') && $(this).nextAll(".rated").length === 0 ){
|
73
|
+
// reseting the value
|
74
|
+
newValue = $item.data('rater-min');
|
75
|
+
event = "reset";
|
76
|
+
}
|
77
|
+
else {
|
78
|
+
// setting a new value
|
79
|
+
newValue = $item.children().index(this) + 1;
|
80
|
+
event = "rated";
|
81
|
+
}
|
82
|
+
|
83
|
+
methods.value.call($item, newValue);
|
84
|
+
$item.trigger(event, [newValue]);
|
85
|
+
});
|
86
|
+
|
87
|
+
$item.data("rater-bound", true);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
},
|
91
|
+
|
92
|
+
value : function(value) {
|
93
|
+
if( typeof(value) != "undefined") {
|
94
|
+
var $ratings = $(this).find(".rating").removeClass("rated");
|
95
|
+
|
96
|
+
if( value > $(this).data('rater-min') ) {
|
97
|
+
$ratings.eq(value - 1).addClass("rated").prevAll().addClass("rated");
|
98
|
+
}
|
99
|
+
|
100
|
+
if ($(this).data('rater-field')) {
|
101
|
+
$($(this).data('rater-field')).val(value);
|
102
|
+
}
|
103
|
+
|
104
|
+
$(this).data("rater-value", value);
|
105
|
+
}
|
106
|
+
else {
|
107
|
+
return $(this).data("rater-value");
|
108
|
+
}
|
109
|
+
},
|
110
|
+
|
111
|
+
readonly : function(value) {
|
112
|
+
if( typeof(value) != "undefined") {
|
113
|
+
$(this).data("rater-readonly", value);
|
114
|
+
|
115
|
+
if (value) {
|
116
|
+
$(this).unbind();
|
117
|
+
$(this).data("rater-bound", false);
|
118
|
+
}
|
119
|
+
else {
|
120
|
+
methods.bindEvents.call(this);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
else {
|
124
|
+
return $(this).data("rater-readonly");
|
125
|
+
}
|
126
|
+
}
|
127
|
+
};
|
128
|
+
|
129
|
+
$.fn.responsiveRater = function(methodOrOptions){
|
130
|
+
if (this.length == 0) return this;
|
131
|
+
|
132
|
+
var args = arguments;
|
133
|
+
|
134
|
+
return this.each(function() {
|
135
|
+
|
136
|
+
if ( methods[methodOrOptions] ) {
|
137
|
+
return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( args, 1 ));
|
138
|
+
} else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
|
139
|
+
return methods.init.apply( this, args );
|
140
|
+
} else {
|
141
|
+
$.error( 'Method ' + method + ' does not exist on jQuery.responsiveRater' );
|
142
|
+
}
|
143
|
+
|
144
|
+
});
|
145
|
+
};
|
146
|
+
|
147
|
+
$.fn.responsiveRater.defaults = { content: "★", min: 0, max: 5, readonly: false };
|
148
|
+
|
149
|
+
$(function () { $('div.responsive-rater').responsiveRater(); });
|
150
|
+
|
151
|
+
}( jQuery ));
|
152
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
.responsive-rater
|
2
|
+
width: 100%
|
3
|
+
//text-align: justify
|
4
|
+
display: inline-block
|
5
|
+
|
6
|
+
.rating
|
7
|
+
cursor: pointer
|
8
|
+
display: inline-block
|
9
|
+
text-align: center
|
10
|
+
font-size: 20px
|
11
|
+
color: rgba(#000,.4)
|
12
|
+
-webkit-transition: all .4s
|
13
|
+
-moz-transition: all .4s
|
14
|
+
-ms-transition: all .4s
|
15
|
+
-o-transition: all .4s
|
16
|
+
transition: all .4s
|
17
|
+
|
18
|
+
&.hover
|
19
|
+
color: rgba(#59950C,1)
|
20
|
+
|
21
|
+
&.rated
|
22
|
+
color: rgba(#59950C,1)
|
23
|
+
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_responsive_rater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Scott Walker
|
9
|
+
- Derek Briggs
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-07-10 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: This project was created because I was not happy with any of the existing
|
16
|
+
rating plugins I found for Rails. Mainly because they all relied on images (usually
|
17
|
+
stars) for the rating display. I think a better approach is to make use of unicode
|
18
|
+
symbols and CSS so that the rating display can easily be made responsive without
|
19
|
+
the associated hassles of using images.
|
20
|
+
email:
|
21
|
+
- swalke16@gmail.com
|
22
|
+
- derek.briggs@me.com
|
23
|
+
executables: []
|
24
|
+
extensions: []
|
25
|
+
extra_rdoc_files: []
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- LICENSE
|
30
|
+
- README.md
|
31
|
+
- Rakefile
|
32
|
+
- lib/generators/rails_responsive_rater/install/install_generator.rb
|
33
|
+
- lib/rails_responsive_rater.rb
|
34
|
+
- lib/rails_responsive_rater/version.rb
|
35
|
+
- lib/rails_responsive_rater/view_helpers/action_view.rb
|
36
|
+
- rails_responsive_rater.gemspec
|
37
|
+
- vendor/assets/javascripts/jquery.responsive-rater.js
|
38
|
+
- vendor/assets/stylesheets/jquery.responsive-rater.css.sass
|
39
|
+
homepage: https://github.com/swalke16/rails_responsive_rater
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.24
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: A simple, responsive, client-side rating system Rails.
|
63
|
+
test_files: []
|
64
|
+
has_rdoc:
|