jakewendt-simply_commentable 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ = Simply Commentable
2
+
3
+ == Usage
4
+
5
+ config.gem 'jakewendt-simply_commentable',
6
+ :source => 'http://rubygems.org'
7
+
8
+ # Create generator to install the migrations, javascript, stylesheets,
9
+ # Rakefile, autotest, initializers, factory initializer
10
+ # config/initializer/simply_commentable_post
11
+ script/generate simply_commentable Post
12
+
13
+ # Generator also creates factory initializer
14
+ # config/initializer/simply_commentable_commenter_user
15
+ script/generate simply_commentable_commenter User
16
+
17
+ == ToDo
18
+
19
+
20
+ == Reference and Credits
21
+
22
+ acts_as_commentable
23
+
24
+ == Gemified with Jeweler
25
+
26
+ vi Rakefile
27
+ rake version:write
28
+
29
+ rake version:bump:patch
30
+ rake version:bump:minor
31
+ rake version:bump:major
32
+
33
+ rake gemspec
34
+
35
+ rake install
36
+ rake release
37
+
38
+ Copyright (c) 2010 [Jake Wendt], released under the MIT license
@@ -0,0 +1,36 @@
1
+ class Comment < ActiveRecord::Base
2
+ belongs_to :commenter, :polymorphic => true, :counter_cache => true
3
+ belongs_to :commentable, :polymorphic => true, :counter_cache => true
4
+
5
+ validates_presence_of :body
6
+ validates_length_of :body, :in => 1..250
7
+ attr_accessible :body
8
+
9
+ # Helper class method to lookup all comments assigned
10
+ # to all commentable types for a given commenter.
11
+ def self.find_comments_by_commenter(commenter)
12
+ find(:all,
13
+ :conditions => ["commenter_type = ? and commenter_id = ?",
14
+ commenter.class.name, commenter.id],
15
+ :order => "created_at DESC"
16
+ )
17
+ end
18
+
19
+ # Helper class method to look up all comments for
20
+ # commentable class name and commentable id.
21
+ # def self.find_comments_for_commentable(commentable_str, commentable_id)
22
+ def self.find_comments_for_commentable(commentable)
23
+ find(:all,
24
+ :conditions => ["commentable_type = ? and commentable_id = ?",
25
+ commentable.class.name, commentable.id],
26
+ :order => "created_at DESC"
27
+ )
28
+ end
29
+
30
+ # Helper class method to look up a commentable object
31
+ # given the commentable class name and id
32
+ # def self.find_commentable(commentable_str, commentable_id)
33
+ def self.find_commentable(commentable)
34
+ commentable.class.name.constantize.find(commentable.id)
35
+ end
36
+ end
data/config/routes.rb ADDED
File without changes
File without changes
@@ -0,0 +1,72 @@
1
+ class SimplyCommentableGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ # See Rails::Generator::Commands::Create
5
+ # rails-2.3.10/lib/rails_generator/commands.rb
6
+ # for code methods for record (Manifest)
7
+ record do |m|
8
+ m.directory('config/autotest')
9
+ m.file('autotest_simply_commentable.rb', 'config/autotest/simply_commentable.rb')
10
+ m.directory('lib/tasks')
11
+ m.file('simply_commentable.rake', 'lib/tasks/simply_commentable.rake')
12
+
13
+ %w( create_comments ).each do |migration|
14
+ m.migration_template "migrations/#{migration}.rb",
15
+ 'db/migrate', :migration_file_name => migration
16
+ end
17
+
18
+ m.directory('public/javascripts')
19
+ Dir["#{File.dirname(__FILE__)}/templates/javascripts/*js"].each{|file|
20
+ f = file.split('/').slice(-2,2).join('/')
21
+ m.file(f, "public/javascripts/#{File.basename(file)}")
22
+ }
23
+
24
+ m.directory('public/stylesheets')
25
+ Dir["#{File.dirname(__FILE__)}/templates/stylesheets/*css"].each{|file|
26
+ f = file.split('/').slice(-2,2).join('/')
27
+ m.file(f, "public/stylesheets/#{File.basename(file)}")
28
+ }
29
+
30
+ # m.directory('test/functional/pages')
31
+ # Dir["#{File.dirname(__FILE__)}/templates/functional/*rb"].each{|file|
32
+ # f = file.split('/').slice(-2,2).join('/')
33
+ # m.file(f, "test/functional/pages/#{File.basename(file)}")
34
+ # }
35
+ # m.directory('test/unit/pages')
36
+ # Dir["#{File.dirname(__FILE__)}/templates/unit/*rb"].each{|file|
37
+ # f = file.split('/').slice(-2,2).join('/')
38
+ # m.file(f, "test/unit/pages/#{File.basename(file)}")
39
+ # }
40
+ end
41
+ end
42
+
43
+ end
44
+ module Rails::Generator::Commands
45
+ class Create
46
+ def migration_template(relative_source,
47
+ relative_destination, template_options = {})
48
+ migration_directory relative_destination
49
+ migration_file_name = template_options[
50
+ :migration_file_name] || file_name
51
+ if migration_exists?(migration_file_name)
52
+ puts "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}: Skipping"
53
+ else
54
+ template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
55
+ end
56
+ end
57
+ end # Create
58
+ class Base
59
+ protected
60
+ # the loop through migrations happens so fast
61
+ # that they all have the same timestamp which
62
+ # won't work when you actually try to migrate.
63
+ # All the timestamps MUST be unique.
64
+ def next_migration_string(padding = 3)
65
+ @s = (!@s.nil?)? @s.to_i + 1 : if ActiveRecord::Base.timestamped_migrations
66
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
67
+ else
68
+ "%.#{padding}d" % next_migration_number
69
+ end
70
+ end
71
+ end # Base
72
+ end
@@ -0,0 +1,2 @@
1
+ # From `script/generate simply_commentable` ...
2
+ require 'simply_commentable/autotest'
@@ -0,0 +1,17 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :comments do |t|
4
+ t.integer :commenter_id
5
+ t.string :commenter_type
6
+ t.integer :commentable_id
7
+ t.string :commentable_type
8
+ t.text :body
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :comments
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # From `script/generate simply_commentable` ...
2
+ unless Gem.source_index.find_name('jakewendt-simply_commentable').empty?
3
+ gem 'jakewendt-simply_commentable'
4
+ require 'simply_commentable/test_tasks'
5
+ end
@@ -0,0 +1 @@
1
+ require 'simply_commentable'
@@ -0,0 +1,7 @@
1
+ module SimplyCommentable
2
+ # predefine namespace
3
+ end
4
+ require 'simply_commentable/base'
5
+ require 'simply_commentable/assertions'
6
+
7
+ require 'active_support'
@@ -0,0 +1,26 @@
1
+ module SimplyCommentable::Assertions
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+ module ClassMethods
6
+ def assert_simply_commentable
7
+ # assert_should_have_many :comments, :polymorphic => true
8
+ test "should be simply commentable" do
9
+ assert model_name.constantize.new.respond_to?(:comments)
10
+ # self.class.assert_should_have_many :comments, :polymorphic => true
11
+ end
12
+ end
13
+ def assert_simply_commentable_commenter
14
+ # assert_should_have_many :comments, :polymorphic => true
15
+ test "should be simply commentable commenter" do
16
+ assert model_name.constantize.new.respond_to?(:comments)
17
+ # self.class.assert_should_have_many :comments, :polymorphic => true
18
+ end
19
+ end
20
+ end
21
+ module InstanceMethods
22
+ end
23
+ end
24
+ require 'active_support'
25
+ require 'active_support/test_case'
26
+ ActiveSupport::TestCase.send(:include,SimplyCommentable::Assertions)
@@ -0,0 +1,29 @@
1
+ module SimplyCommentable::Base
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def simply_commentable_commenter
9
+
10
+ has_many :comments,
11
+ :as => :commenter,
12
+ :dependent => :destroy,
13
+ :order => 'created_at ASC'
14
+
15
+ end
16
+ def simply_commentable
17
+
18
+ has_many :comments,
19
+ :as => :commentable,
20
+ :dependent => :destroy,
21
+ :order => 'created_at ASC'
22
+
23
+ end
24
+ end
25
+
26
+ end # module SimplyCommentable::Base
27
+
28
+ require 'active_record'
29
+ ActiveRecord::Base.send(:include, SimplyCommentable::Base)
@@ -0,0 +1 @@
1
+ Dir["#{File.dirname(__FILE__)}/../tasks/**/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,38 @@
1
+ module SimplyCommentable;end
2
+ namespace :test do
3
+ namespace :units do
4
+ Rake::TestTask.new(:simply_commentable => "db:test:prepare") do |t|
5
+ t.pattern = File.expand_path(File.join(
6
+ File.dirname(__FILE__),'/../../test/unit/commentable/*_test.rb'))
7
+ t.libs << "test"
8
+ t.verbose = true
9
+ end
10
+ end
11
+ namespace :functionals do
12
+ Rake::TestTask.new(:simply_commentable => "db:test:prepare") do |t|
13
+ t.pattern = File.expand_path(File.join(
14
+ File.dirname(__FILE__),'/../../test/functional/commentable/*_test.rb'))
15
+ t.libs << "test"
16
+ t.verbose = true
17
+ end
18
+ end
19
+ end
20
+ Rake::Task['test:functionals'].prerequisites.unshift(
21
+ "test:functionals:simply_commentable" )
22
+ Rake::Task['test:units'].prerequisites.unshift(
23
+ "test:units:simply_commentable" )
24
+
25
+ # I thought of possibly just including this file
26
+ # but that would make __FILE__ different.
27
+ # Hmmm
28
+
29
+ #
30
+ # used in simply_helpful's rake test:coverage to run gem's
31
+ # tests in the context of the application
32
+ #
33
+ @gem_test_dirs ||= []
34
+ @gem_test_dirs << File.expand_path(File.join(File.dirname(__FILE__),
35
+ '/../../test/unit/commentable/'))
36
+ @gem_test_dirs << File.expand_path(File.join(File.dirname(__FILE__),
37
+ '/../../test/functional/commentable/'))
38
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simply_commentable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,11 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ def redirections
4
+ @redirections ||= HashWithIndifferentAccess.new({
5
+ :not_be_user => {
6
+ :redirect_to => user_path(current_user)
7
+ }
8
+ })
9
+ end
10
+
11
+ end
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
data/test/factories.rb ADDED
@@ -0,0 +1,7 @@
1
+ Factory.define :comment do |f|
2
+ f.body "My Comment"
3
+ end
4
+ Factory.define :post do |f|
5
+ end
6
+ Factory.define :user do |f|
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class SimplyCommentable::CommentTest < ActiveSupport::TestCase
4
+
5
+ assert_should_belong_to :commenter, :polymorphic => true
6
+ assert_should_belong_to :commentable, :polymorphic => true
7
+ assert_should_require :body
8
+ # assert_should_require_attribute_length(:body,:in => 1..250)
9
+ assert_should_require_attribute_length(:body,:minimum => 1, :maximum => 250)
10
+ assert_should_not_protect_attribute :body
11
+ assert_should_protect_attribute :commenter_id
12
+ assert_should_protect_attribute :commenter_type
13
+ assert_should_protect_attribute :commentable_id
14
+ assert_should_protect_attribute :commentable_type
15
+
16
+ test "should find_comments_by_commenter" do
17
+ pending
18
+ end
19
+
20
+ test "should find_comments_for_commentable" do
21
+ pending
22
+ end
23
+
24
+ test "should find_commentable" do
25
+ pending
26
+ end
27
+
28
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class PostTest < ActiveSupport::TestCase
4
+
5
+ assert_simply_commentable
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+
5
+ assert_simply_commentable_commenter
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jakewendt-simply_commentable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 1
10
+ version: 0.2.1
11
+ platform: ruby
12
+ authors:
13
+ - George 'Jake' Wendt
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-26 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 2
32
+ version: "2"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: jakewendt-rails_extension
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: longer description of your gem
50
+ email: github@jakewendt.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - README.rdoc
57
+ files:
58
+ - app/models/comment.rb
59
+ - config/routes.rb
60
+ - generators/simply_commentable/USAGE
61
+ - generators/simply_commentable/simply_commentable_generator.rb
62
+ - generators/simply_commentable/templates/autotest_simply_commentable.rb
63
+ - generators/simply_commentable/templates/migrations/create_comments.rb
64
+ - generators/simply_commentable/templates/simply_commentable.rake
65
+ - lib/jakewendt-simply_commentable.rb
66
+ - lib/simply_commentable.rb
67
+ - lib/simply_commentable/assertions.rb
68
+ - lib/simply_commentable/base.rb
69
+ - lib/simply_commentable/tasks.rb
70
+ - lib/simply_commentable/test_tasks.rb
71
+ - lib/tasks/simply_commentable_tasks.rake
72
+ - README.rdoc
73
+ - test/app/controllers/application_controller.rb
74
+ - test/app/models/post.rb
75
+ - test/app/models/user.rb
76
+ - test/factories.rb
77
+ - test/unit/commentable/comment_test.rb
78
+ - test/unit/post_test.rb
79
+ - test/unit/user_test.rb
80
+ has_rdoc: true
81
+ homepage: http://github.com/jakewendt/simply_commentable
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.6.2
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: one-line summary of your gem
114
+ test_files:
115
+ - test/app/controllers/application_controller.rb
116
+ - test/app/models/post.rb
117
+ - test/app/models/user.rb
118
+ - test/factories.rb
119
+ - test/unit/commentable/comment_test.rb
120
+ - test/unit/post_test.rb
121
+ - test/unit/user_test.rb