parrot 0.0.1 → 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 (90) hide show
  1. data/Gemfile +3 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README.md +26 -0
  4. data/Rakefile +30 -1
  5. data/app/controllers/parrot/application_controller.rb +9 -0
  6. data/app/controllers/parrot/comments_controller.rb +57 -0
  7. data/app/models/parrot/comment.rb +37 -0
  8. data/app/views/parrot/comments/_comment.html.erb +13 -0
  9. data/app/views/parrot/comments/_form.html.erb +22 -0
  10. data/app/views/parrot/comments/index.html.erb +1 -0
  11. data/app/views/parrot/comments/new.html.erb +1 -0
  12. data/config/initializers/parrot.rb +1 -0
  13. data/config/locales/parrot.en.yml +8 -0
  14. data/config/locales/parrot.es.yml +8 -0
  15. data/config/routes.rb +4 -0
  16. data/db/migrate/20120429124205_create_parrot_comments.rb +15 -0
  17. data/lib/generators/parrot/parrot_generator.rb +19 -0
  18. data/lib/parrot.rb +17 -1
  19. data/lib/parrot/model_additions.rb +11 -0
  20. data/lib/parrot/version.rb +1 -1
  21. data/parrot.gemspec +5 -4
  22. data/test/dummy/Rakefile +7 -0
  23. data/test/dummy/app/assets/javascripts/application.js +15 -0
  24. data/test/dummy/app/assets/javascripts/posts.js +2 -0
  25. data/test/dummy/app/assets/javascripts/users.js +2 -0
  26. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  27. data/test/dummy/app/assets/stylesheets/posts.css +4 -0
  28. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  29. data/test/dummy/app/assets/stylesheets/users.css +4 -0
  30. data/test/dummy/app/controllers/application_controller.rb +3 -0
  31. data/test/dummy/app/controllers/posts_controller.rb +83 -0
  32. data/test/dummy/app/controllers/users_controller.rb +83 -0
  33. data/test/dummy/app/helpers/application_helper.rb +2 -0
  34. data/test/dummy/app/helpers/posts_helper.rb +2 -0
  35. data/test/dummy/app/helpers/users_helper.rb +2 -0
  36. data/test/dummy/app/mailers/.gitkeep +0 -0
  37. data/test/dummy/app/models/.gitkeep +0 -0
  38. data/test/dummy/app/models/post.rb +5 -0
  39. data/test/dummy/app/models/user.rb +14 -0
  40. data/test/dummy/app/views/layouts/application.html.erb +17 -0
  41. data/test/dummy/app/views/posts/_form.html.erb +25 -0
  42. data/test/dummy/app/views/posts/edit.html.erb +6 -0
  43. data/test/dummy/app/views/posts/index.html.erb +25 -0
  44. data/test/dummy/app/views/posts/new.html.erb +5 -0
  45. data/test/dummy/app/views/posts/show.html.erb +17 -0
  46. data/test/dummy/app/views/users/_form.html.erb +21 -0
  47. data/test/dummy/app/views/users/edit.html.erb +6 -0
  48. data/test/dummy/app/views/users/index.html.erb +23 -0
  49. data/test/dummy/app/views/users/new.html.erb +5 -0
  50. data/test/dummy/app/views/users/show.html.erb +10 -0
  51. data/test/dummy/config.ru +4 -0
  52. data/test/dummy/config/application.rb +57 -0
  53. data/test/dummy/config/boot.rb +10 -0
  54. data/test/dummy/config/database.yml +25 -0
  55. data/test/dummy/config/environment.rb +5 -0
  56. data/test/dummy/config/environments/development.rb +38 -0
  57. data/test/dummy/config/environments/production.rb +67 -0
  58. data/test/dummy/config/environments/test.rb +37 -0
  59. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  60. data/test/dummy/config/initializers/devise.rb +223 -0
  61. data/test/dummy/config/initializers/inflections.rb +15 -0
  62. data/test/dummy/config/initializers/mime_types.rb +5 -0
  63. data/test/dummy/config/initializers/secret_token.rb +7 -0
  64. data/test/dummy/config/initializers/session_store.rb +8 -0
  65. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  66. data/test/dummy/config/locales/devise.en.yml +57 -0
  67. data/test/dummy/config/locales/en.yml +5 -0
  68. data/test/dummy/config/routes.rb +8 -0
  69. data/test/dummy/db/migrate/20120429002406_create_posts.rb +10 -0
  70. data/test/dummy/db/migrate/20120429002431_create_parrot_comments.rb +12 -0
  71. data/test/dummy/db/migrate/20120429124205_devise_create_users.rb +49 -0
  72. data/test/dummy/db/schema.rb +51 -0
  73. data/test/dummy/lib/assets/.gitkeep +0 -0
  74. data/test/dummy/log/.gitkeep +0 -0
  75. data/test/dummy/public/404.html +26 -0
  76. data/test/dummy/public/422.html +26 -0
  77. data/test/dummy/public/500.html +25 -0
  78. data/test/dummy/public/favicon.ico +0 -0
  79. data/test/dummy/script/rails +6 -0
  80. data/test/dummy/test/fixtures/posts.yml +9 -0
  81. data/test/dummy/test/fixtures/users.yml +11 -0
  82. data/test/dummy/test/functional/posts_controller_test.rb +49 -0
  83. data/test/dummy/test/functional/users_controller_test.rb +49 -0
  84. data/test/dummy/test/unit/helpers/posts_helper_test.rb +4 -0
  85. data/test/dummy/test/unit/helpers/users_helper_test.rb +4 -0
  86. data/test/dummy/test/unit/post_test.rb +7 -0
  87. data/test/dummy/test/unit/user_test.rb +7 -0
  88. data/test/parrot_test.rb +7 -0
  89. data/test/test_helper.rb +15 -0
  90. metadata +189 -5
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in parrot.gemspec
4
4
  gemspec
5
+
6
+ # jquery-rails is used by the dummy application
7
+ gem "jquery-rails"
@@ -0,0 +1,21 @@
1
+ == MIT License
2
+
3
+ Copyright (c) 2012 [Tute Costa - tutecosta@gmail.com]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1 +1,27 @@
1
1
  # Parrot Gem: Simple commenting solution for Rails 3 apps.
2
+
3
+ # Installation
4
+
5
+ 1. Add <tt>gem 'parrot'</tt> to your Gemfile
6
+ 2. Run <tt>rake parrot:install:migrations</tt>
7
+ 3. Run <tt>rake db:migrate</tt>
8
+ 4. Run <tt>rails g parrot MODEL_NAME</tt>
9
+ 5. Add subresources to routes
10
+
11
+ resources RESOURCE_NAME do
12
+ resources :parrot_comments, controller: 'parrot/comments', path: 'comments'
13
+ end
14
+
15
+ 6. Add commenting relation to your commenters:
16
+
17
+ has_many :comments, :class_name => Parrot::Comment, :foreign_key => :author_id
18
+
19
+ # Notes
20
+
21
+ * By default it calls to_s on commenter name. You may want to alias it to name
22
+ for example, so it doesn't show the Ruby "ugly" object.
23
+
24
+ # To-do
25
+
26
+ * Flash responders working?
27
+ * How to reopen parrot's subclasses from parent applications?
data/Rakefile CHANGED
@@ -1 +1,30 @@
1
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ begin
4
+ require 'rdoc/task'
5
+ rescue LoadError
6
+ require 'rdoc/rdoc'
7
+ require 'rake/rdoctask'
8
+ RDoc::Task = Rake::RDocTask
9
+ end
10
+
11
+ Rake::RDocTask.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Parrot'
14
+ rdoc.options << '--line-numbers' << '--inline-source'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ Bundler::GemHelper.install_tasks
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'lib'
25
+ t.libs << 'test'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = false
28
+ end
29
+
30
+ task :default => :test
@@ -0,0 +1,9 @@
1
+ module Parrot
2
+ class ApplicationController < ActionController::Base
3
+ # Reading default_url_options from parent app ApplicationController
4
+ def default_url_options(options = {})
5
+ app_controller = ::ApplicationController.new
6
+ app_controller.default_url_options if app_controller.respond_to? :default_url_options
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,57 @@
1
+ module Parrot
2
+ class CommentsController < ApplicationController
3
+ respond_to :html
4
+
5
+ def index
6
+ @comments = Comment.where(commentable_type: commentable_type.classify, commentable_id: commentable_id)
7
+ respond_with @comments
8
+ end
9
+
10
+ def new
11
+ @comment = Comment.new
12
+ respond_with @comment
13
+ end
14
+
15
+ def create
16
+ @comment = Comment.new(params[:parrot_comment])
17
+ @comment.commentable_type = commentable_type.classify
18
+ @comment.commentable_id = commentable_id
19
+ @comment.author_id = current_user.id
20
+ @comment.save
21
+ @commentable_type = commentable_type
22
+ respond_with @comment, :location => commentable
23
+ end
24
+
25
+ def destroy
26
+ @comment = current_user.comments.find params[:id]
27
+ @comment.destroy
28
+ respond_with @comment, :location => commentable
29
+ end
30
+
31
+ # Following methods should belong to ApplicationController
32
+ def commentable_fk
33
+ commentable_fk = params.select{|k,v| k =~ /_id/ }.keys.first
34
+ end
35
+
36
+ def commentable_type
37
+ commentable_fk.gsub('_id', '')
38
+ end
39
+
40
+ def commentable_id
41
+ id = params[commentable_fk]
42
+ if id.to_i.to_s == id # Numeric id
43
+ id.to_i
44
+ else
45
+ commentable(id).id # Slugged (text, but we store integers)
46
+ end
47
+ end
48
+
49
+ def commentable(id = nil)
50
+ commentable_type.classify.constantize.find(id || commentable_id)
51
+ end
52
+
53
+ def debug(object)
54
+ logger.warn "DEBUGGING: #{object.inspect}"
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,37 @@
1
+ module Parrot
2
+ class Comment < ActiveRecord::Base
3
+ self.table_name = :parrot_comments # FIXME: Not needed if I can isolate the gem
4
+
5
+ belongs_to :commentable, :polymorphic => true
6
+ belongs_to :author, :class_name => Parrot.author_class
7
+
8
+ before_save :cache_author_data
9
+
10
+ attr_accessible :commentable, :body
11
+
12
+ validates_presence_of :commentable, :body, :author_id
13
+
14
+ def self.siblings_of(other_comment)
15
+ where(
16
+ commentable_type: other_comment.commentable_type,
17
+ commentable_id: other_comment.commentable_id
18
+ )
19
+ end
20
+
21
+ def self.following(other_comment)
22
+ siblings_of(other_comment).where("id > #{other_comment.id}")
23
+ end
24
+
25
+ def author
26
+ Parrot.author_class.find_by_id author_id
27
+ end
28
+
29
+ # An author may delete it's account, we cache/store it's sensitive data
30
+ def cache_author_data
31
+ return if author.nil?
32
+ [:name, :email, :phone].each do |method|
33
+ self.send(:"author_#{method}=", author.send(method)) if author.respond_to?(method)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ <div id="c_<%= comment.id %>" class="comment">
2
+ <h5>
3
+ <%= t('date.formats.on') %>
4
+ <%= parse_date(comment.created_at, t('date.formats.datetime')) %>
5
+ <% if current_user.present? && current_user == comment.author %>
6
+ <%= t('orders.show.you_wrote').html_safe %>:
7
+ <%= button_to t('buttons.destroy'), [comment.commentable, comment], confirm: t('messages.are_you_sure'), method: :delete, class: 'btn btn-danger' %>
8
+ <% else %>
9
+ <strong><%= comment.author %></strong> <%= t('parrot.wrote') %>:
10
+ <% end %>
11
+ </h5>
12
+ <p><%= comment.body %></p>
13
+ </div>
@@ -0,0 +1,22 @@
1
+ <% @comment ||= Parrot::Comment.new %>
2
+ <%= form_for([object, @comment]) do |f| %>
3
+ <% if @comment.errors.any? %>
4
+ <div id="error_explanation">
5
+ <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
6
+
7
+ <ul>
8
+ <% @comment.errors.full_messages.each do |msg| %>
9
+ <li><%= msg %></li>
10
+ <% end %>
11
+ </ul>
12
+ </div>
13
+ <% end %>
14
+
15
+ <div class="field">
16
+ <%= f.label :body, t('parrot.new_comment') %><br />
17
+ <%= f.text_area :body %>
18
+ </div>
19
+ <div class="actions">
20
+ <%= f.submit t('parrot.send_comment'), class: 'btn btn-success' %>
21
+ </div>
22
+ <% end %>
@@ -0,0 +1 @@
1
+ <%= render @comments %>
@@ -0,0 +1 @@
1
+ <%= render 'form', object: @commentable_type %>
@@ -0,0 +1 @@
1
+ Parrot.author_class = 'User'
@@ -0,0 +1,8 @@
1
+ en:
2
+ activerecord:
3
+ models:
4
+ comment: Comment
5
+ parrot:
6
+ new_comment: "New comment"
7
+ send_comment: "Send"
8
+ wrote: wrote
@@ -0,0 +1,8 @@
1
+ es:
2
+ activerecord:
3
+ models:
4
+ comment: Comentario
5
+ parrot:
6
+ new_comment: "Nuevo comentario"
7
+ send_comment: "Enviar comentario"
8
+ wrote: escribió
@@ -0,0 +1,4 @@
1
+ Parrot::Engine.routes.draw do
2
+ resources :comments
3
+ root :to => 'comments#index'
4
+ end
@@ -0,0 +1,15 @@
1
+ class CreateParrotComments < ActiveRecord::Migration
2
+ def change
3
+ create_table :parrot_comments do |t|
4
+ t.integer :author_id
5
+ t.string :author_name
6
+ t.string :author_email
7
+ t.string :author_phone
8
+ t.string :commentable_type
9
+ t.integer :commentable_id
10
+ t.text :body
11
+ t.integer :parent_id
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module Parrot
2
+ module Generators
3
+ class ParrotGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def model_exists?
7
+ File.exists?(File.join(destination_root, model_path))
8
+ end
9
+
10
+ def model_path
11
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
12
+ end
13
+
14
+ def inject_parrot_content
15
+ inject_into_class(model_path, class_name, " parrot_comments\n\n") if model_exists?
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,4 +1,20 @@
1
- require "parrot/version"
1
+ require 'parrot/version'
2
2
 
3
3
  module Parrot
4
+ mattr_accessor :author_class
5
+ def self.author_class
6
+ @@author_class.constantize
7
+ end
8
+
9
+ class Engine < Rails::Engine
10
+ engine_name 'parrot'
11
+ # isolate_namespace Parrot # FIXME: tries application url helpers (undefined method `post_comments_path')
12
+
13
+ initializer 'parrot' do |app|
14
+ ActiveSupport.on_load(:active_record) do
15
+ require File.join(File.dirname(__FILE__), 'parrot', 'model_additions')
16
+ ::ActiveRecord::Base.send :include, Parrot::Model
17
+ end
18
+ end
19
+ end
4
20
  end
@@ -0,0 +1,11 @@
1
+ module Parrot
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def parrot_comments
7
+ has_many :comments, :class_name => Parrot::Comment, :as => :commentable
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Parrot
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
@@ -1,4 +1,5 @@
1
1
  $:.push File.expand_path("../lib", __FILE__)
2
+
2
3
  require "parrot/version"
3
4
 
4
5
  Gem::Specification.new do |s|
@@ -7,13 +8,13 @@ Gem::Specification.new do |s|
7
8
  s.authors = ["Tute Costa"]
8
9
  s.email = "tutecosta@gmail.com"
9
10
  s.homepage = "http://github.com/tute/parrot"
10
- s.summary = %q{Simple commenting solution for Rails 3 apps.}
11
+ s.summary = %q{Simple commenting Rails Engine. Adds commentable behavior to any model easily.}
11
12
  s.description = %q{Add simple comments to any Rails resource.}
12
13
 
13
14
  s.files = `git ls-files`.split("\n").reject{|f| f =~ /^\./ }
14
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
- s.require_paths = ["lib"]
16
16
 
17
- # s.add_dependency "rest-client"
18
- # s.add_development_dependency "rspec"
17
+ s.add_dependency 'rails'
18
+ s.add_development_dependency 'sqlite3'
19
+ s.add_development_dependency 'devise'
19
20
  end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -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,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -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
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }