Skip to content

Commit d0f6949

Browse files
Run Rubocop to do some style cleanup and consolidation
1 parent 5a41cf7 commit d0f6949

14 files changed

+225
-178
lines changed

Gemfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development do
6-
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
7-
gem 'sqlite3', :platforms => :ruby
6+
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
7+
gem 'sqlite3', platforms: :ruby
88
end

Rakefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ Bundler::GemHelper.install_tasks
33

44
require 'rspec/core/rake_task'
55

6-
desc "Run specs"
6+
desc 'Run specs'
77
RSpec::Core::RakeTask.new(:spec)
88

9-
task :install => [:package] do
10-
sh %{sudo gem install pkg/#{NAME}-#{GEM_VERSION}}
9+
task install: [:package] do
10+
sh %(sudo gem install pkg/#{NAME}-#{GEM_VERSION})
1111
end
1212

1313
desc 'Default: run specs.'
14-
task :default => :spec
14+
task default: :spec

acts_as_commentable_with_threading.gemspec

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Gem::Specification.new do |s|
2-
s.name = "acts_as_commentable_with_threading"
3-
s.version = "2.0.1"
4-
s.date = "2015-12-22"
5-
s.summary = "Polymorphic comments Rails gem - Rails 4+ only"
6-
s.email = "[email protected]"
7-
s.homepage = "http://github.com/elight/acts_as_commentable_with_threading"
8-
s.description = "Polymorphic threaded comments Rails gem for Rails 4+"
9-
s.authors = ["Evan Light", "Jack Dempsey", "Xelipe", "xxx"]
2+
s.name = 'acts_as_commentable_with_threading'
3+
s.version = '2.0.1'
4+
s.date = '2015-12-22'
5+
s.summary = 'Polymorphic comments Rails gem - Rails 4+ only'
6+
s.email = '[email protected]'
7+
s.homepage = 'http://github.com/elight/acts_as_commentable_with_threading'
8+
s.description = 'Polymorphic threaded comments Rails gem for Rails 4+'
9+
s.authors = ['Evan Light', 'Jack Dempsey', 'Xelipe', 'xxx']
1010
s.files = `git ls-files`.split("\n")
1111
s.test_files = `git ls-files -- spec/*`.split("\n")
1212

lib/acts_as_commentable_with_threading.rb

+39-37
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,55 @@
1212
# ActsAsCommentableWithThreading
1313
module Acts #:nodoc:
1414
module CommentableWithThreading #:nodoc:
15-
extend ActiveSupport::Concern
15+
extend ActiveSupport::Concern
1616

17-
module ClassMethods
18-
def acts_as_commentable
19-
has_many :comment_threads, :class_name => "Comment", :as => :commentable
20-
before_destroy { |record| record.root_comments.destroy_all }
21-
include Acts::CommentableWithThreading::LocalInstanceMethods
22-
extend Acts::CommentableWithThreading::SingletonMethods
17+
module ClassMethods
18+
def acts_as_commentable
19+
has_many :comment_threads, class_name: 'Comment', as: :commentable
20+
before_destroy { |record| record.root_comments.destroy_all }
21+
include Acts::CommentableWithThreading::LocalInstanceMethods
22+
extend Acts::CommentableWithThreading::SingletonMethods
23+
end
2324
end
24-
end
2525

26-
# This module contains class methods
27-
module SingletonMethods
28-
# Helper method to lookup for comments for a given object.
29-
# This method is equivalent to obj.comments.
30-
def find_comments_for(obj)
31-
Comment.where(:commentable_id => obj.id, :commentable_type => obj.class.base_class.name).order('created_at DESC')
32-
end
26+
# This module contains class methods
27+
module SingletonMethods
28+
# Helper method to lookup for comments for a given object.
29+
# This method is equivalent to obj.comments.
30+
def find_comments_for(obj)
31+
Comment.where(commentable_id: obj.id,
32+
commentable_type: obj.class.base_class.name)
33+
.order('created_at DESC')
34+
end
3335

34-
# Helper class method to lookup comments for
35-
# the mixin commentable type written by a given user.
36-
# This method is NOT equivalent to Comment.find_comments_for_user
37-
def find_comments_by_user(user)
38-
commentable = self.base_class.name.to_s
39-
Comment.where(:user_id => user.id, :commentable_type => commentable).order('created_at DESC')
36+
# Helper class method to lookup comments for
37+
# the mixin commentable type written by a given user.
38+
# This method is NOT equivalent to Comment.find_comments_for_user
39+
def find_comments_by_user(user)
40+
commentable = base_class.name.to_s
41+
Comment.where(user_id: user.id, commentable_type: commentable)
42+
.order('created_at DESC')
43+
end
4044
end
41-
end
42-
43-
module LocalInstanceMethods
4445

45-
# Helper method to display only root threads, no children/replies
46-
def root_comments
47-
self.comment_threads.where(:parent_id => nil)
48-
end
46+
module LocalInstanceMethods
47+
# Helper method to display only root threads, no children/replies
48+
def root_comments
49+
comment_threads.where(parent_id: nil)
50+
end
4951

50-
# Helper method to sort comments by date
51-
def comments_ordered_by_submitted
52-
Comment.where(:commentable_id => id, :commentable_type => self.class.name).order('created_at DESC')
53-
end
52+
# Helper method to sort comments by date
53+
def comments_ordered_by_submitted
54+
Comment.where(commentable_id: id, commentable_type: self.class.name)
55+
.order('created_at DESC')
56+
end
5457

55-
# Helper method that defaults the submitted time.
56-
def add_comment(comment)
57-
comments << comment
58+
# Helper method that defaults the submitted time.
59+
def add_comment(comment)
60+
comments << comment
61+
end
5862
end
5963
end
60-
61-
end
6264
end
6365

6466
ActiveRecord::Base.send(:include, Acts::CommentableWithThreading)
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
class ActsAsCommentableUpgradeMigrationGenerator < Rails::Generators::Base
22
include Rails::Generators::Migration
3-
3+
44
source_root File.expand_path('../templates', __FILE__)
5-
6-
# *for the life of me*, i can't figure out why this isn't implemented elsewhere.
5+
6+
# *for the life of me*, i can't figure out why this isn't
7+
# implemented elsewhere.
78
def self.next_migration_number(dirname)
89
if ActiveRecord::Base.timestamped_migrations
9-
Time.now.utc.strftime("%Y%m%d%H%M%S")
10+
Time.now.utc.strftime('%Y%m%d%H%M%S')
1011
else
11-
"%.3d" % (current_migration_number(dirname) + 1)
12+
format('%.3d', (current_migration_number(dirname) + 1))
1213
end
1314
end
1415

1516
def manifest
16-
migration_template 'migration.rb', 'db/migrate/acts_as_commentable_upgrade_migration.rb'
17+
migration_template 'migration.rb',
18+
'db/migrate/acts_as_commentable_upgrade_migration.rb'
1719
copy_file 'comment.rb', 'app/models/comment.rb'
1820
end
1921
end

lib/generators/acts_as_commentable_upgrade_migration/comment.rb

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Comment < ActiveRecord::Base
2-
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
2+
acts_as_nested_set scope: [:commentable_id, :commentable_type]
33

4-
validates :body, :presence => true
5-
validates :user, :presence => true
4+
validates :body, presence: true
5+
validates :user, presence: true
66

77
# NOTE: install the acts_as_votable plugin if you
88
# want user to vote on the quality of comments.
9-
#acts_as_votable
9+
# acts_as_votable
1010

11-
belongs_to :commentable, :polymorphic => true
11+
belongs_to :commentable, polymorphic: true
1212

1313
# NOTE: Comments belong to a user
1414
belongs_to :user
@@ -18,26 +18,27 @@ class Comment < ActiveRecord::Base
1818
# example in readme
1919
def self.build_from(obj, user_id, comment)
2020
new \
21-
:commentable => obj,
22-
:body => comment,
23-
:user_id => user_id
21+
commentable: obj,
22+
body: comment,
23+
user_id: user_id
2424
end
2525

26-
#helper method to check if a comment has children
26+
# helper method to check if a comment has children
2727
def has_children?
28-
self.children.size > 0
28+
children.size > 0
2929
end
3030

3131
# Helper class method to lookup all comments assigned
3232
# to all commentable types for a given user.
3333
scope :find_comments_by_user, lambda { |user|
34-
where(:user_id => user.id).order('created_at DESC')
34+
where(user_id: user.id).order('created_at DESC')
3535
}
3636

3737
# Helper class method to look up all comments for
3838
# commentable class name and commentable id.
39-
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
40-
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
39+
scope :find_comments_for_commentable, lambda { |type, id|
40+
where(commentable_type: type.to_s,
41+
commentable_id: id).order('created_at DESC')
4142
}
4243

4344
# Helper class method to look up a commentable object

lib/generators/acts_as_commentable_upgrade_migration/templates/migration.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ def self.up
66
add_column :comments, :lft, :integer
77
add_column :comments, :rgt, :integer
88
add_column :comments, :updated_at, :datetime
9-
9+
1010
add_index :comments, :commentable_id
1111
end
12-
12+
1313
def self.down
1414
rename_column :comments, :body, :comment
1515
remove_column :comments, :subject
1616
remove_column :comments, :parent_id
1717
remove_column :comments, :lft
1818
remove_column :comments, :rgt
1919
remove_column :comments, :updated_at
20-
20+
2121
remove_index :comments, :commentable_id
2222
end
2323
end
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
class ActsAsCommentableWithThreadingMigrationGenerator < Rails::Generators::Base
22
include Rails::Generators::Migration
3-
3+
44
source_root File.expand_path('../templates', __FILE__)
5-
5+
66
# Complete wtf that this isn't provided elsewhere.
77
def self.next_migration_number(dirname)
88
if ActiveRecord::Base.timestamped_migrations
9-
Time.now.utc.strftime("%Y%m%d%H%M%S")
9+
Time.now.utc.strftime('%Y%m%d%H%M%S')
1010
else
11-
"%.3d" % (current_migration_number(dirname) + 1)
11+
format('%.3d', (current_migration_number(dirname) + 1))
1212
end
1313
end
1414

1515
def manifest
16-
migration_template 'migration.rb', 'db/migrate/acts_as_commentable_with_threading_migration.rb'
16+
migration_template 'migration.rb',
17+
'db/migrate/acts_as_commentable_with_threading_migration.rb'
1718
copy_file 'comment.rb', 'app/models/comment.rb'
1819
end
1920
end

lib/generators/acts_as_commentable_with_threading_migration/templates/comment.rb

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Comment < ActiveRecord::Base
2-
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
2+
acts_as_nested_set scope: [:commentable_id, :commentable_type]
33

4-
validates :body, :presence => true
5-
validates :user, :presence => true
4+
validates :body, presence: true
5+
validates :user, presence: true
66

77
# NOTE: install the acts_as_votable plugin if you
88
# want user to vote on the quality of comments.
9-
#acts_as_votable
9+
# acts_as_votable
1010

11-
belongs_to :commentable, :polymorphic => true
11+
belongs_to :commentable, polymorphic: true
1212

1313
# NOTE: Comments belong to a user
1414
belongs_to :user
@@ -18,26 +18,27 @@ class Comment < ActiveRecord::Base
1818
# example in readme
1919
def self.build_from(obj, user_id, comment)
2020
new \
21-
:commentable => obj,
22-
:body => comment,
23-
:user_id => user_id
21+
commentable: obj,
22+
body: comment,
23+
user_id: user_id
2424
end
2525

26-
#helper method to check if a comment has children
26+
# helper method to check if a comment has children
2727
def has_children?
28-
self.children.any?
28+
children.any?
2929
end
3030

3131
# Helper class method to lookup all comments assigned
3232
# to all commentable types for a given user.
3333
scope :find_comments_by_user, lambda { |user|
34-
where(:user_id => user.id).order('created_at DESC')
34+
where(user_id: user.id).order('created_at DESC')
3535
}
3636

3737
# Helper class method to look up all comments for
3838
# commentable class name and commentable id.
39-
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
40-
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
39+
scope :find_comments_for_commentable, lambda { |type, id|
40+
where(commentable_type: type.to_s, commentable_id: id)
41+
.order('created_at DESC')
4142
}
4243

4344
# Helper class method to look up a commentable object

lib/generators/acts_as_commentable_with_threading_migration/templates/migration.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class ActsAsCommentableWithThreadingMigration < ActiveRecord::Migration
22
def self.up
3-
create_table :comments, :force => true do |t|
3+
create_table :comments, force: true do |t|
44
t.integer :commentable_id
55
t.string :commentable_type
66
t.string :title
77
t.text :body
88
t.string :subject
9-
t.integer :user_id, :null => false
9+
t.integer :user_id, null: false
1010
t.integer :parent_id, :lft, :rgt
1111
t.timestamps
1212
end

0 commit comments

Comments
 (0)