Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rails4 support and also option to get more conditional hashtaggable Value #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/simple_hashtag/hashtag.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "protected_attributes"
module SimpleHashtag
class Hashtag < ActiveRecord::Base
self.table_name = "simple_hashtag_hashtags"

has_many :hashtaggings

validates :name, uniqueness: true
attr_accessible :name

# TODO Beef up the regex (ie.:what if content is HTML)
# this is how Twitter does it:
Expand All @@ -27,8 +29,8 @@ def name
read_attribute(:name).downcase
end

def hashtaggables
self.hashtaggings.includes(:hashtaggable).collect { |h| h.hashtaggable }
def hashtaggables(conditions = {})
self.hashtaggings.includes(:hashtaggable).where(conditions).collect {|h| h.unscoped.hashtaggable}
end

def hashtagged_types
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_hashtag/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SimpleHashtag
VERSION = "0.1.8"
VERSION = "0.2.0"
end
1 change: 1 addition & 0 deletions simple_hashtag.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec-rails"
spec.add_development_dependency "sqlite3"
spec.add_dependency "protected_attributes"
end
1 change: 1 addition & 0 deletions spec/dummy/app/models/picture.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Picture < ActiveRecord::Base
include SimpleHashtag::Hashtaggable
hashtaggable_attribute :caption
attr_accessible :caption
end
1 change: 1 addition & 0 deletions spec/dummy/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Post < ActiveRecord::Base
include SimpleHashtag::Hashtaggable
attr_accessible :body
end
2 changes: 1 addition & 1 deletion spec/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# config.active_record.auto_explain_threshold_in_seconds = 0.5

# Do not compress assets
config.assets.compress = false
Expand Down
7 changes: 7 additions & 0 deletions spec/simple_hashtag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
tag.hashtagged_ids_for_type("Post").should include(Post.last.id)
tag.hashtagged_ids_for_type("Picture").should include(Picture.last.id)
end

it "with there conditions" do
tag = SimpleHashtag::Hashtag.find_by_name("book")
tag.hashtaggables.size.should eq 2
tag.hashtaggables(:hashtaggable_id => Post.last.id, :hashtaggable_type => 'Post').size.should eq 1
end

end

it "is destroyed whith parent" do
Expand Down