cartoonist-tags 0.0.11

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 ADDED
@@ -0,0 +1,2 @@
1
+ *~
2
+ /cartoonist-tags*.gem
@@ -0,0 +1,22 @@
1
+ class Admin::TagsController < CartoonistController
2
+ before_filter :preview!, :only => [:show]
3
+ before_filter :ensure_ssl!
4
+ before_filter :check_admin!
5
+
6
+ def show
7
+ @tag = Tag.find params[:id].to_i
8
+ render "tags/show", :layout => "tags"
9
+ end
10
+
11
+ def create
12
+ entity_tag = EntityTag.create_tag params
13
+ redirect_to entity_tag.entity.entity_edit_url
14
+ end
15
+
16
+ def destroy
17
+ entity_tag = EntityTag.tagged_entity params
18
+ entity = entity_tag.entity
19
+ entity_tag.untag!
20
+ redirect_to entity.entity_edit_url
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ class TagsController < CartoonistController
2
+ def show
3
+ @tag = Tag.find params[:id].to_i
4
+
5
+ cache_page_as "tags/#{@tag.id}.#{cache_type}.tmp.html" do
6
+ render
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ class EntityTag < ActiveRecord::Base
2
+ include BelongsToEntity
3
+ belongs_to :tag
4
+ validate :entity_doesnt_change, :on => :update
5
+ attr_accessible :entity_id, :entity_type, :tag_id
6
+
7
+ def posted_header
8
+ if entity.kind_of?(Postable) && entity.posted_at
9
+ "#{entity.posted_at.to_time.localtime.strftime "%m/%d/%Y"}:"
10
+ end
11
+ end
12
+
13
+ def untag!
14
+ destroy
15
+ remaining = EntityTag.where(:tag_id => tag.id).count
16
+ tag.destroy if remaining == 0
17
+ end
18
+
19
+ class << self
20
+ def create_tag(params)
21
+ tag = Tag.with_label params[:label]
22
+ result = where(:tag_id => tag.id, :entity_id => params[:entity_id].to_i, :entity_type => params[:entity_type]).first
23
+ result = create :tag_id => tag.id, :entity_id => params[:entity_id].to_i, :entity_type => params[:entity_type] unless result
24
+ result
25
+ end
26
+
27
+ def tagged_entity(params)
28
+ where(:tag_id => params[:id].to_i, :entity_id => params[:entity_id].to_i, :entity_type => params[:entity_type]).first
29
+ end
30
+
31
+ def tags_for(entity)
32
+ tag_ids = where(:entity_id => entity.id, :entity_type => entity.entity_type).pluck(:tag_id)
33
+ Tag.where(:id => tag_ids).order(:label).all
34
+ end
35
+ end
36
+ end
data/app/models/tag.rb ADDED
@@ -0,0 +1,43 @@
1
+ class Tag < ActiveRecord::Base
2
+ has_many :entity_tags
3
+ attr_accessible :label
4
+
5
+ def previewable_url(preview)
6
+ if preview
7
+ "/admin/tags/#{id}"
8
+ else
9
+ "/tags/#{id}"
10
+ end
11
+ end
12
+
13
+ def shown_entity_tags(preview)
14
+ @shown_entity_tags ||= entity_tags.all.select do |entity_tag|
15
+ preview || !entity_tag.entity.kind_of?(Postable) || entity_tag.entity.posted?
16
+ end.sort do |a, b|
17
+ if a.entity.kind_of?(Postable) && a.entity.posted_at && b.entity.kind_of?(Postable) && b.entity.posted_at
18
+ b.entity.posted_at.to_date <=> a.entity.posted_at.to_date
19
+ elsif a.entity.kind_of?(Postable) && a.entity.posted_at
20
+ 1
21
+ elsif b.entity.kind_of?(Postable) && b.entity.posted_at
22
+ -1
23
+ else
24
+ a.description <=> b.description
25
+ end
26
+ end
27
+
28
+ raise ActiveRecord::RecordNotFound.new("No records found!") if @shown_entity_tags.empty?
29
+ @shown_entity_tags
30
+ end
31
+
32
+ class << self
33
+ def existing
34
+ order(:label).all
35
+ end
36
+
37
+ def with_label(label)
38
+ tag = where(:label => label).first
39
+ tag = create :label => label unless tag
40
+ tag
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ <% tags = EntityTag.tags_for entity %>
2
+ <% existing_tags = Tag.existing %>
3
+ <% unless tags.empty? %>
4
+ <h2><%= t "admin.tags.entity_tags.existing_tags" %></h2>
5
+
6
+ <ul>
7
+ <% tags.each do |tag| %>
8
+ <li>
9
+ <%= tag.label %>
10
+ <%= form_tag "/admin/tags/#{tag.id}", :method => :delete, :class => "inline" do %>
11
+ <input type="hidden" name="entity_id" value="<%= entity.id %>" />
12
+ <input type="hidden" name="entity_type" value="<%= entity.entity_type %>" />
13
+ <input type="submit" value="<%= t "admin.tags.entity_tags.delete_tag" %>" />
14
+ <% end %>
15
+ </li>
16
+ <% end %>
17
+ </ul>
18
+ <% end %>
19
+ <%= form_tag "/admin/tags", :method => :post do %>
20
+ <input type="hidden" name="entity_id" value="<%= entity.id %>" />
21
+ <input type="hidden" name="entity_type" value="<%= entity.entity_type %>" />
22
+ <input type="text" name="label" />
23
+ <input type="submit" value="<%= t "admin.tags.entity_tags.add_tag" %>" />
24
+ <% end %>
25
+ <% unless existing_tags.empty? %>
26
+ <%= form_tag "/admin/tags", :method => :post do %>
27
+ <input type="hidden" name="entity_id" value="<%= entity.id %>" />
28
+ <input type="hidden" name="entity_type" value="<%= entity.entity_type %>" />
29
+ <select name="label">
30
+ <% existing_tags.each do |tag| %>
31
+ <option value="<%= tag.label %>"><%= tag.label %></option>
32
+ <% end %>
33
+ </select>
34
+ <input type="submit" value="<%= t "admin.tags.entity_tags.add_tag" %>" />
35
+ <% end %>
36
+ <% end %>
@@ -0,0 +1,17 @@
1
+ <% content_for :content_class, "tags-content" %>
2
+
3
+ <% content_for :content do %>
4
+ <div class="tags-page">
5
+ <h1><%= yield :title %></h1>
6
+
7
+ <%= yield %>
8
+ </div>
9
+ <% end %>
10
+
11
+ <% content_for :admin_content do %>
12
+ <% if preview? %>
13
+ <p><a href="/admin/main">admin</a></p>
14
+ <% end %>
15
+ <% end %>
16
+
17
+ <%= render :template => "layouts/cartoonist" %>
@@ -0,0 +1,12 @@
1
+ <% tags = EntityTag.tags_for entity %>
2
+ <% unless tags.empty? %>
3
+ <div class="tags">
4
+ <ul>
5
+ <% tags.each do |tag| %>
6
+ <li>
7
+ <a href="<%= tag.previewable_url preview? %>"><%= tag.label %></a>
8
+ </li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
@@ -0,0 +1,10 @@
1
+ <% content_for :title, "Things tagged '#{@tag.label}'" %>
2
+
3
+ <ul>
4
+ <% @tag.shown_entity_tags(preview?).each do |entity_tag| %>
5
+ <li>
6
+ <%= entity_tag.posted_header %>
7
+ <a href="<%= entity_tag.entity.entity_relative_previewable_url preview? %>"><%= entity_tag.description %></a>
8
+ </li>
9
+ <% end %>
10
+ </ul>
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ raise "Cannot find version file!" unless File.exists?(File.join(File.dirname(__FILE__), "../CARTOONIST_VERSION"))
3
+ cartoonist_version = File.read(File.join(File.dirname(__FILE__), "../CARTOONIST_VERSION")).strip
4
+ s.name = "cartoonist-tags"
5
+ s.version = cartoonist_version
6
+ s.date = Time.now.strftime "%Y-%m-%d"
7
+ s.summary = "Cartoonist Tags"
8
+ s.description = "Plugin for tagging cartoonist entities."
9
+ s.authors = ["Mike Virata-Stone"]
10
+ s.email = "reasonnumber@gmail.com"
11
+ s.files = `git ls-files`.split("\n")
12
+ s.require_paths = ["lib"]
13
+ s.homepage = "http://reasonnumber.com/cartoonist"
14
+ s.add_dependency "cartoonist", cartoonist_version
15
+ end
@@ -0,0 +1,7 @@
1
+ en:
2
+ admin:
3
+ tags:
4
+ entity_tags:
5
+ add_tag: Add Tag
6
+ delete_tag: Delete
7
+ existing_tags: "Existing Tags:"
@@ -0,0 +1,19 @@
1
+ class CreateTags < ActiveRecord::Migration
2
+ def change
3
+ create_table :tags do |t|
4
+ t.string :label, :null => false
5
+ t.timestamps
6
+ end
7
+
8
+ add_index :tags, :label, :unique => true
9
+
10
+ create_table :entity_tags do |t|
11
+ t.integer :entity_id, :null => false
12
+ t.string :entity_type, :null => false
13
+ t.integer :tag_id, :null => false
14
+ t.timestamps
15
+ end
16
+
17
+ add_index :entity_tags, [:entity_id, :entity_type, :tag_id], :unique => true
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ module CartoonistTags
2
+ class EntityHooks
3
+ class << self
4
+ def edit_entity_before_partial
5
+ "admin/tags/entity_tags"
6
+ end
7
+
8
+ def show_entity_before_partial
9
+ "tags/entity_tags"
10
+ end
11
+ end
12
+ end
13
+
14
+ class Engine < ::Rails::Engine
15
+ Cartoonist::Migration.add_for self
16
+ Cartoonist::Entity.register_hooks CartoonistTags::EntityHooks
17
+
18
+ Cartoonist::Backup.for :tags do
19
+ Tag.order(:id)
20
+ end
21
+
22
+ Cartoonist::Backup.for :entity_tags do
23
+ EntityTag.order(:id)
24
+ end
25
+
26
+ Cartoonist::Routes.add do
27
+ resources :tags
28
+
29
+ namespace :admin do
30
+ resources :tags
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module CartoonistTags
2
+ require "cartoonist-tags/engine"
3
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cartoonist-tags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.11
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Virata-Stone
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cartoonist
16
+ requirement: &13712940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.11
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *13712940
25
+ description: Plugin for tagging cartoonist entities.
26
+ email: reasonnumber@gmail.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - .gitignore
32
+ - app/controllers/admin/tags_controller.rb
33
+ - app/controllers/tags_controller.rb
34
+ - app/models/entity_tag.rb
35
+ - app/models/tag.rb
36
+ - app/views/admin/tags/_entity_tags.html.erb
37
+ - app/views/layouts/tags.html.erb
38
+ - app/views/tags/_entity_tags.html.erb
39
+ - app/views/tags/show.html.erb
40
+ - cartoonist-tags.gemspec
41
+ - config/locales/en.yml
42
+ - db/migrate/20120525062923_create_tags.rb
43
+ - lib/cartoonist-tags.rb
44
+ - lib/cartoonist-tags/engine.rb
45
+ homepage: http://reasonnumber.com/cartoonist
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.17
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Cartoonist Tags
69
+ test_files: []