tinted_tags 0.0.2

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.
Files changed (75) hide show
  1. data/.gitignore +8 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +19 -0
  4. data/Gemfile.lock +133 -0
  5. data/MIT-LICENSE.md +20 -0
  6. data/README.md +67 -0
  7. data/Rakefile +24 -0
  8. data/app/assets/images/tinted_tags/.gitkeep +0 -0
  9. data/app/assets/javascripts/tinted_tags/application.js +15 -0
  10. data/app/assets/stylesheets/tinted_tags/application.css +13 -0
  11. data/app/controllers/tinted_tags/application_controller.rb +4 -0
  12. data/app/helpers/tinted_tags/application_helper.rb +4 -0
  13. data/app/views/layouts/tinted_tags/application.html.erb +14 -0
  14. data/config/routes.rb +2 -0
  15. data/lib/generators/tinted_tags/migration/migration_generator.rb +38 -0
  16. data/lib/generators/tinted_tags/migration/templates/active_record/migration.rb +5 -0
  17. data/lib/tinted_tags.rb +28 -0
  18. data/lib/tinted_tags/engine.rb +8 -0
  19. data/lib/tinted_tags/tag_tinter.rb +42 -0
  20. data/lib/tinted_tags/version.rb +3 -0
  21. data/lib/view_helpers/view_helpers.rb +11 -0
  22. data/script/rails +8 -0
  23. data/spec/dummy/README.rdoc +261 -0
  24. data/spec/dummy/Rakefile +7 -0
  25. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  26. data/spec/dummy/app/assets/javascripts/posts.js +2 -0
  27. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  28. data/spec/dummy/app/assets/stylesheets/posts.css +4 -0
  29. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  30. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  31. data/spec/dummy/app/controllers/posts_controller.rb +83 -0
  32. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  33. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  34. data/spec/dummy/app/mailers/.gitkeep +0 -0
  35. data/spec/dummy/app/models/.gitkeep +0 -0
  36. data/spec/dummy/app/models/post.rb +5 -0
  37. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  38. data/spec/dummy/app/views/posts/_form.html.erb +21 -0
  39. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  40. data/spec/dummy/app/views/posts/index.html.erb +23 -0
  41. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  42. data/spec/dummy/app/views/posts/show.html.erb +10 -0
  43. data/spec/dummy/config.ru +4 -0
  44. data/spec/dummy/config/application.rb +59 -0
  45. data/spec/dummy/config/boot.rb +10 -0
  46. data/spec/dummy/config/database.yml +25 -0
  47. data/spec/dummy/config/environment.rb +5 -0
  48. data/spec/dummy/config/environments/development.rb +37 -0
  49. data/spec/dummy/config/environments/production.rb +67 -0
  50. data/spec/dummy/config/environments/test.rb +37 -0
  51. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/dummy/config/initializers/inflections.rb +15 -0
  53. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  54. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  55. data/spec/dummy/config/initializers/session_store.rb +8 -0
  56. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/dummy/config/locales/en.yml +5 -0
  58. data/spec/dummy/config/routes.rb +7 -0
  59. data/spec/dummy/db/migrate/20130128180847_acts_as_taggable_on_migration.rb +30 -0
  60. data/spec/dummy/db/migrate/20130128181205_create_posts.rb +9 -0
  61. data/spec/dummy/db/migrate/20130128184436_add_tint_to_tags.rb +5 -0
  62. data/spec/dummy/db/schema.rb +40 -0
  63. data/spec/dummy/lib/assets/.gitkeep +0 -0
  64. data/spec/dummy/log/.gitkeep +0 -0
  65. data/spec/dummy/public/404.html +26 -0
  66. data/spec/dummy/public/422.html +26 -0
  67. data/spec/dummy/public/500.html +25 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/dummy/script/rails +6 -0
  70. data/spec/generators/tinted_tags/migration/migration_generator_spec.rb +22 -0
  71. data/spec/spec_helper.rb +27 -0
  72. data/spec/tinted_tags/tag_tinter_spec.rb +43 -0
  73. data/spec/tinted_tags/tinted_tags_spec.rb +16 -0
  74. data/tinted_tags.gemspec +26 -0
  75. metadata +200 -0
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .bundle/
2
+ log/*.log
3
+ *.gem
4
+ pkg/
5
+ .DS_Store
6
+ tmp
7
+ spec/dummy/db/*.sqlite3
8
+ spec/dummy/log/*.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in tinted_tags.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+ gem 'ammeter'
11
+ gem 'acts-as-taggable-on', '~> 2.3.1'
12
+
13
+ # Declare any dependencies that are still in development here instead of in
14
+ # your gemspec. These might include edge Rails or gems from your path or
15
+ # Git. Remember to move these dependencies to your gemspec before releasing
16
+ # your gem to rubygems.org.
17
+
18
+ # To use debugger
19
+ # gem 'debugger'
data/Gemfile.lock ADDED
@@ -0,0 +1,133 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tinted_tags (0.0.1)
5
+ acts-as-taggable-on (~> 2.3.1)
6
+ compass
7
+ rails (>= 3.2.11)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ actionmailer (3.2.11)
13
+ actionpack (= 3.2.11)
14
+ mail (~> 2.4.4)
15
+ actionpack (3.2.11)
16
+ activemodel (= 3.2.11)
17
+ activesupport (= 3.2.11)
18
+ builder (~> 3.0.0)
19
+ erubis (~> 2.7.0)
20
+ journey (~> 1.0.4)
21
+ rack (~> 1.4.0)
22
+ rack-cache (~> 1.2)
23
+ rack-test (~> 0.6.1)
24
+ sprockets (~> 2.2.1)
25
+ activemodel (3.2.11)
26
+ activesupport (= 3.2.11)
27
+ builder (~> 3.0.0)
28
+ activerecord (3.2.11)
29
+ activemodel (= 3.2.11)
30
+ activesupport (= 3.2.11)
31
+ arel (~> 3.0.2)
32
+ tzinfo (~> 0.3.29)
33
+ activeresource (3.2.11)
34
+ activemodel (= 3.2.11)
35
+ activesupport (= 3.2.11)
36
+ activesupport (3.2.11)
37
+ i18n (~> 0.6)
38
+ multi_json (~> 1.0)
39
+ acts-as-taggable-on (2.3.3)
40
+ rails (~> 3.0)
41
+ ammeter (0.2.8)
42
+ activesupport (>= 3.0)
43
+ railties (>= 3.0)
44
+ rspec (>= 2.2)
45
+ rspec-rails (>= 2.2)
46
+ arel (3.0.2)
47
+ builder (3.0.4)
48
+ chunky_png (1.2.5)
49
+ compass (0.12.2)
50
+ chunky_png (~> 1.2)
51
+ fssm (>= 0.2.7)
52
+ sass (~> 3.1)
53
+ diff-lcs (1.1.3)
54
+ erubis (2.7.0)
55
+ fssm (0.2.8.1)
56
+ hike (1.2.1)
57
+ i18n (0.6.1)
58
+ journey (1.0.4)
59
+ jquery-rails (2.2.0)
60
+ railties (>= 3.0, < 5.0)
61
+ thor (>= 0.14, < 2.0)
62
+ json (1.7.6)
63
+ mail (2.4.4)
64
+ i18n (>= 0.4.0)
65
+ mime-types (~> 1.16)
66
+ treetop (~> 1.4.8)
67
+ mime-types (1.19)
68
+ multi_json (1.5.0)
69
+ polyglot (0.3.3)
70
+ rack (1.4.4)
71
+ rack-cache (1.2)
72
+ rack (>= 0.4)
73
+ rack-ssl (1.3.2)
74
+ rack
75
+ rack-test (0.6.2)
76
+ rack (>= 1.0)
77
+ rails (3.2.11)
78
+ actionmailer (= 3.2.11)
79
+ actionpack (= 3.2.11)
80
+ activerecord (= 3.2.11)
81
+ activeresource (= 3.2.11)
82
+ activesupport (= 3.2.11)
83
+ bundler (~> 1.0)
84
+ railties (= 3.2.11)
85
+ railties (3.2.11)
86
+ actionpack (= 3.2.11)
87
+ activesupport (= 3.2.11)
88
+ rack-ssl (~> 1.3.2)
89
+ rake (>= 0.8.7)
90
+ rdoc (~> 3.4)
91
+ thor (>= 0.14.6, < 2.0)
92
+ rake (10.0.3)
93
+ rdoc (3.12)
94
+ json (~> 1.4)
95
+ rspec (2.12.0)
96
+ rspec-core (~> 2.12.0)
97
+ rspec-expectations (~> 2.12.0)
98
+ rspec-mocks (~> 2.12.0)
99
+ rspec-core (2.12.2)
100
+ rspec-expectations (2.12.1)
101
+ diff-lcs (~> 1.1.3)
102
+ rspec-mocks (2.12.2)
103
+ rspec-rails (2.12.2)
104
+ actionpack (>= 3.0)
105
+ activesupport (>= 3.0)
106
+ railties (>= 3.0)
107
+ rspec-core (~> 2.12.0)
108
+ rspec-expectations (~> 2.12.0)
109
+ rspec-mocks (~> 2.12.0)
110
+ sass (3.2.5)
111
+ sprockets (2.2.2)
112
+ hike (~> 1.2)
113
+ multi_json (~> 1.0)
114
+ rack (~> 1.0)
115
+ tilt (~> 1.1, != 1.3.0)
116
+ sqlite3 (1.3.7)
117
+ thor (0.17.0)
118
+ tilt (1.3.3)
119
+ treetop (1.4.12)
120
+ polyglot
121
+ polyglot (>= 0.3.1)
122
+ tzinfo (0.3.35)
123
+
124
+ PLATFORMS
125
+ ruby
126
+
127
+ DEPENDENCIES
128
+ acts-as-taggable-on (~> 2.3.1)
129
+ ammeter
130
+ jquery-rails
131
+ rspec-rails (~> 2.0)
132
+ sqlite3
133
+ tinted_tags!
data/MIT-LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Ben Woodward
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.md ADDED
@@ -0,0 +1,67 @@
1
+ # TintedTags
2
+
3
+ This plugin is to be used with
4
+ [acts_as_taggable_on](https://github.com/mbleigh/acts-as-taggable-on).
5
+ TintedTags allows you to create a tag cloud where tag popularity is
6
+ illustrated with colour.
7
+ It achieves this by adding a :tint attribute to the Tag model, and allows you to calculate
8
+ a colour (in the form of a hex code) for each tag based on its
9
+ popularity. These hex codes are added to the tags in the view via
10
+ inline-css.
11
+
12
+ Tags are updated via an ```after_save``` filter on a TintedTags object.
13
+
14
+ ## Installation
15
+
16
+ ### Rails 3.x
17
+
18
+ To use it, add it to your Gemfile:
19
+
20
+ ```ruby
21
+ gem 'acts-as-taggable-on', '~> 2.3.1'
22
+ gem 'tinted_tags'
23
+ ```
24
+
25
+ #### Post Installation
26
+
27
+ ```shell
28
+ rails generate tinted_tags:migration
29
+ rake db:migrate
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Model
35
+
36
+ ```ruby
37
+ acts_as_taggable_on
38
+ tinted_tags base: '#ffffff', tint: '#000000'
39
+ ```
40
+
41
+ ### Controller
42
+
43
+ ```ruby
44
+ @tags = Post.tag_counts_on(:tags).order('count desc')
45
+ ```
46
+
47
+ ### Views
48
+
49
+ ```ruby
50
+ tinted_tag_cloud
51
+ ```
52
+ returns ..
53
+
54
+ ```html
55
+ <ul class="tinted-tag-cloud">
56
+ <li style="background-color: #ffffff">one</li>
57
+ <li style="background-color: #7f7f7f">two</li>
58
+ </ul>
59
+ ```
60
+
61
+ ## Author
62
+
63
+ * [Ben Woodward](https://github.com/benwoodward)
64
+
65
+ Copyright (c) Ben Woodward (http://benw.me/)
66
+ Copyright Released under the [MIT
67
+ license](https://github.com/benwoodward/tinted_tags/blob/master/MIT-LICENSE.md)
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler.setup :default, :development
9
+
10
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
11
+ load 'rails/tasks/engine.rake'
12
+
13
+ require 'rspec/core/rake_task'
14
+
15
+ desc "Run all specs in spec directory (excluding plugin specs)"
16
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
17
+
18
+ RSpec::Core::RakeTask.new(:spec) do |spec|
19
+ spec.pattern = FileList['spec/**/*_spec.rb']
20
+ end
21
+
22
+ task default: :spec
23
+
24
+ Bundler::GemHelper.install_tasks
File without changes
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module TintedTags
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module TintedTags
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>TintedTags</title>
5
+ <%= stylesheet_link_tag "tinted_tags/application", :media => "all" %>
6
+ <%= javascript_include_tag "tinted_tags/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ TintedTags::Engine.routes.draw do
2
+ end
@@ -0,0 +1,38 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module TintedTags
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ desc "Generates migration for Tag tints"
9
+
10
+ def self.orm
11
+ Rails::Generators.options[:rails][:orm]
12
+ end
13
+
14
+ def self.source_root
15
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
16
+ end
17
+
18
+ def self.orm_has_migration?
19
+ [:active_record].include? orm
20
+ end
21
+
22
+ def self.next_migration_number(dirname)
23
+ if ActiveRecord::Base.timestamped_migrations
24
+ migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
25
+ migration_number += 1
26
+ migration_number.to_s
27
+ else
28
+ "%.3d" % (current_migration_number(dirname) + 1)
29
+ end
30
+ end
31
+
32
+ def create_migration_file
33
+ if self.class.orm_has_migration?
34
+ migration_template 'migration.rb', 'db/migrate/tinted_tags_migration'
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ class TintedTagsMigration < ActiveRecord::Migration
2
+ def change
3
+ add_column :tags, :tint, :string
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ require "tinted_tags/engine"
2
+ require "tinted_tags/tag_tinter"
3
+ require "view_helpers/view_helpers"
4
+
5
+ module TintedTags
6
+
7
+ def tinted_tags(opts={})
8
+ class_attribute :opts
9
+ self.opts = opts
10
+
11
+ class_eval do
12
+
13
+ attr_accessible :tag_list
14
+ after_save :update_tints, if: :tag_list_changed?
15
+
16
+ private
17
+
18
+ def update_tints
19
+ TagTinter.new(self.class, opts).update_tints
20
+ end
21
+ end
22
+ end
23
+
24
+ if defined?(ActiveRecord::Base)
25
+ ActiveRecord::Base.extend TintedTags
26
+ end
27
+ end
28
+
@@ -0,0 +1,8 @@
1
+ module TintedTags
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace TintedTags
4
+ initializer 'tinted tags' do
5
+ ActionView::Base.send :include, TintedTags::ViewHelpers
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,42 @@
1
+ require 'compass'
2
+
3
+ module TintedTags
4
+ class TagTinter
5
+ def initialize(klass, opts={})
6
+ @base = opts[:base]
7
+ @tint = opts[:tint]
8
+ @strategy = opts[:strategy]
9
+ @klass = klass
10
+ end
11
+
12
+ def update_tints
13
+ @klass.tag_counts_on(:tags).each do |tag|
14
+ percent = percentage(tag.count)
15
+ tag.tint = evaluate("mix(#{@base}, #{@tint}, #{percent})")
16
+ tag.save
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def percentage(count)
23
+ return percentage_of_total(count) unless @strategy == :rated_as_range
24
+ percentage_within_range(count)
25
+ end
26
+
27
+ def percentage_of_total(count)
28
+ total = @klass.tag_counts_on(:tags).map(&:count).inject(:+)
29
+ ((count * 100) / total).round(2)
30
+ end
31
+
32
+ def percentage_within_range(count)
33
+ max = @klass.tag_counts_on(:tags).order('count desc').first.count
34
+ min = @klass.tag_counts_on(:tags).order('count desc').last.count
35
+ ((100 * (count-min)) / (max-min)).round(2)
36
+ end
37
+
38
+ def evaluate(value)
39
+ Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new).to_s
40
+ end
41
+ end
42
+ end