has_comments 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 John Felix
1
+ Copyright (c) 2011 [name of plugin creator]
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README ADDED
@@ -0,0 +1,13 @@
1
+ HasComments
2
+ ===========
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2011 [name of plugin creator], released under the MIT license
data/Rakefile CHANGED
@@ -1,46 +1,23 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "has_comments"
18
- gem.homepage = "http://github.com/johnfelix/has_comments"
19
- gem.license = "MIT"
20
- gem.summary = "comments gem"
21
- gem.description = "comments"
22
- gem.email = "felix1985@gmail.com"
23
- gem.authors = ["John Felix"]
24
- gem.version = "0.2.0"
25
- # dependencies defined in Gemfile
26
- end
27
- Jeweler::RubygemsDotOrgTasks.new
28
-
1
+ #!/usr/bin/env rake
29
2
  require 'rake/testtask'
30
- Rake::TestTask.new(:test) do |test|
31
- test.libs << 'lib' << 'test'
32
- test.pattern = 'test/**/test_*.rb'
33
- test.verbose = true
34
- end
3
+ require 'rdoc/task'
35
4
 
5
+ desc 'Default: run unit tests.'
36
6
  task :default => :test
37
7
 
38
- require 'rake/rdoctask'
39
- Rake::RDocTask.new do |rdoc|
40
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
8
+ desc 'Test the has_comments plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
41
15
 
16
+ desc 'Generate documentation for the has_comments plugin.'
17
+ RDoc::Task.new(:rdoc) do |rdoc|
42
18
  rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "has_comments #{version}"
44
- rdoc.rdoc_files.include('README*')
19
+ rdoc.title = 'HasComments'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
45
22
  rdoc.rdoc_files.include('lib/**/*.rb')
46
23
  end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__)+'/lib/has_comments.rb'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate has_comments Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,22 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class HasCommentsGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def create_model_file
8
+ template "comment.rb","app/models/comment.rb"
9
+ end
10
+
11
+ def create_migration_file
12
+ migration_template "create_comments.rb","db/migrate/create_comments.rb"
13
+ end
14
+
15
+ def create_controller_file
16
+ template "comments_controller.rb","app/controllers/comments_controller.rb"
17
+ end
18
+
19
+ def self.next_migration_number(path)
20
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class Comment < ActiveRecord::Base
2
+ belongs_to :comment, :polymorphic => true
3
+ end
@@ -0,0 +1,42 @@
1
+ class CommentsController < ActionController::Base
2
+ respond_to :html, :xml, :json
3
+
4
+ def index
5
+ @comments = Comment.where(:article_id => params[:article_id]).all
6
+ respond_with(@comments)
7
+ end
8
+
9
+ def new
10
+ @comment = Comment.new
11
+ respond_with(@comments)
12
+ end
13
+
14
+ def create
15
+ @comment = Comment.new(params[:comment])
16
+ if @comment.save
17
+ redirect_to article_url(params[:article_id])
18
+ else
19
+ render :new
20
+ end
21
+ end
22
+
23
+ def edit
24
+ @comment = Comment.find(params[:id])
25
+ respond_with(@comment)
26
+ end
27
+
28
+ def update
29
+ @comment = Comment.find( params[:id] )
30
+ if @comment.update_attributes(params[:comment])
31
+ redirect_to article_url(params[:article_id])
32
+ else
33
+ render :edit
34
+ end
35
+ end
36
+
37
+ def destroy
38
+ @comment = Comment.find( params[:id] )
39
+ @comment.destroy
40
+ redirect_to article_url(params[:article_id])
41
+ end
42
+ end
@@ -0,0 +1,10 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def change
3
+ create_table :comments do|t|
4
+ t.text :content
5
+ t.string :title
6
+ t.references :comment, :polymorphic => true
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
data/lib/has_comments.rb CHANGED
@@ -1 +1,29 @@
1
- require "methods"
1
+ module HasComments
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def has_comments
8
+ has_many :comments, :as => :comment
9
+ send :include, InstanceMethods
10
+ end
11
+ end
12
+
13
+ module InstanceMethods
14
+ def method_missing(method, *args )
15
+ case method
16
+ when /first_\d+_comments/i
17
+ self.comments.limit($1)
18
+ when /last_\d+_comments/i
19
+ self.comments.limit($1).order("created_at desc")
20
+ else
21
+ super
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ class ActiveRecord::Base
28
+ include HasComments
29
+ end
data/test/database.yml ADDED
@@ -0,0 +1,9 @@
1
+ # vendor/plugins/yaffle/test/database.yml
2
+
3
+ sqlite:
4
+ :adapter: sqlite
5
+ :database: vendor/plugins/has_comments/test/has_comments_plugin.sqlite.db
6
+
7
+ sqlite3:
8
+ :adapter: sqlite3
9
+ :database: vendor/plugins/has_comments/test/has_comments_plugin.sqlite3.db
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__)+'/test_helper'
2
+
3
+ class HasCommentsTest < ActiveSupport::TestCase
4
+ load_schema
5
+
6
+ class Article < ActiveRecord::Base
7
+ has_comments
8
+ end
9
+
10
+ class Comment < ActiveRecord::Base
11
+ belongs_to :comment, :polymorphic => true
12
+ end
13
+
14
+ def setup
15
+ @article = Article.create!({:title => "title", :content => "content"})
16
+ @first_comment = Comment.create!({:title => "title", :content => "content", :comment => @article})
17
+ @second_comment = Comment.create!({:title => "title", :content => "content", :comment => @article})
18
+ end
19
+
20
+ def test_existence_for_first_comments_method
21
+ assert_equal 2, @article.first_2_comments.count
22
+ end
23
+
24
+ def test_existence_for_last_comments_method
25
+ assert_equal 2, @article.last_2_comments.count
26
+ end
27
+
28
+ def teardown
29
+ @article.comments.destroy_all
30
+ @article.destroy
31
+ end
32
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,9 @@
1
+ # vendor/plugins/yaffle/test/schema.rb
2
+
3
+ ActiveRecord::Schema.define(:version => 0) do
4
+ create_table :articles, :force => true do |t|
5
+ t.string :title
6
+ t.string :content
7
+ t.timestamps
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ # vendor/plugins/yaffle/test/test_helper.rb
2
+
3
+ ENV['RAILS_ENV'] = 'test'
4
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
5
+
6
+ require 'test/unit'
7
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
8
+
9
+ def load_schema
10
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
11
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
12
+
13
+ db_adapter = ENV['DB']
14
+
15
+ # no db passed, try one of these fine config-free DBs before bombing.
16
+ db_adapter ||=
17
+ begin
18
+ require 'rubygems'
19
+ require 'sqlite'
20
+ 'sqlite'
21
+ rescue MissingSourceFile
22
+ begin
23
+ require 'sqlite3'
24
+ 'sqlite3'
25
+ rescue MissingSourceFile
26
+ end
27
+ end
28
+
29
+ if db_adapter.nil?
30
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
31
+ end
32
+
33
+ ActiveRecord::Base.establish_connection(config[db_adapter])
34
+ load(File.dirname(__FILE__) + "/schema.rb")
35
+ require File.dirname(__FILE__) + '../../init'
36
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_comments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,50 +9,33 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-20 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: bundler
16
- requirement: &2161524820 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 1.0.0
22
- type: :development
23
- prerelease: false
24
- version_requirements: *2161524820
25
- - !ruby/object:Gem::Dependency
26
- name: jeweler
27
- requirement: &2161524220 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ~>
31
- - !ruby/object:Gem::Version
32
- version: 1.6.4
33
- type: :development
34
- prerelease: false
35
- version_requirements: *2161524220
36
- description: comments
12
+ date: 2011-08-22 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
37
15
  email: felix1985@gmail.com
38
16
  executables: []
39
17
  extensions: []
40
18
  extra_rdoc_files:
41
- - LICENSE.txt
42
- - README.rdoc
19
+ - README
43
20
  files:
44
- - .document
45
- - Gemfile
46
- - Gemfile.lock
47
- - LICENSE.txt
48
- - README.rdoc
49
- - Rakefile
21
+ - MIT-LICENSE
22
+ - README
50
23
  - lib/has_comments.rb
51
- - test/helper.rb
52
- - test/test_has_comments.rb
53
- homepage: http://github.com/johnfelix/has_comments
54
- licenses:
55
- - MIT
24
+ - lib/generators/has_comments_generator.rb
25
+ - lib/generators/templates/comment.rb
26
+ - lib/generators/templates/comments_controller.rb
27
+ - lib/generators/templates/create_comments.rb
28
+ - lib/generators/USAGE
29
+ - Rakefile
30
+ - test/database.yml
31
+ - test/has_comments_test.rb
32
+ - test/schema.rb
33
+ - test/test_helper.rb
34
+ - uninstall.rb
35
+ - init.rb
36
+ - install.rb
37
+ homepage: http://github.com/has_comments
38
+ licenses: []
56
39
  post_install_message:
57
40
  rdoc_options: []
58
41
  require_paths:
@@ -74,5 +57,5 @@ rubyforge_project:
74
57
  rubygems_version: 1.8.8
75
58
  signing_key:
76
59
  specification_version: 3
77
- summary: comments gem
60
+ summary: has_comments
78
61
  test_files: []
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- group :development do
4
- gem "bundler", "~> 1.0.0"
5
- gem "jeweler", "~> 1.6.4"
6
- end
data/Gemfile.lock DELETED
@@ -1,16 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- git (1.2.5)
5
- jeweler (1.6.4)
6
- bundler (~> 1.0)
7
- git (>= 1.2.5)
8
- rake
9
- rake (0.9.2)
10
-
11
- PLATFORMS
12
- ruby
13
-
14
- DEPENDENCIES
15
- bundler (~> 1.0.0)
16
- jeweler (~> 1.6.4)
data/README.rdoc DELETED
@@ -1,19 +0,0 @@
1
- = has_comments
2
-
3
- Description goes here.
4
-
5
- == Contributing to has_comments
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2011 John Felix. See LICENSE.txt for
18
- further details.
19
-
data/test/helper.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- begin
4
- Bundler.setup(:default, :development)
5
- rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
9
- end
10
- require 'test/unit'
11
-
12
- $LOAD_PATH.unshift(File.dirname(__FILE__))
13
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
- require 'has_comments'
15
-
16
- class Test::Unit::TestCase
17
- end
@@ -1,7 +0,0 @@
1
- require 'helper'
2
-
3
- class TestHasComments < Test::Unit::TestCase
4
- def test_something_for_real
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end