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

Add option to only generate hashtags if a specific condition is met #7

Open
wants to merge 1 commit 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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ class Picture < ActiveRecord::Base
end
```

If you only want hashtags created based on conditions,
you can create a guard with `hashtag_if`:

```ruby
class Post < ActiveRecord::Base
include SimpleHashtag::Hashtaggable

hashtag_if ->(post) { post.body =~ /ruby/ }
end

# or...

class Post < ActiveRecord::Base
include SimpleHashtag::Hashtaggable

hashtag_if :about_ruby?

def about_ruby?
body =~ /ruby/
end
end
```


From here on, if your text contains a hashtag, say _#RubyRocks_,
_Simple Hasthag_ will find it, store it in a table and retreive it and its associated object if asked.
Helpers are also available to create a link when displaying the text.
Expand Down
21 changes: 21 additions & 0 deletions lib/simple_hashtag/hashtaggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module SimpleHashtag
module Hashtaggable
extend ActiveSupport::Concern

class InvalidHashtagGuard < ArgumentError; end

included do
has_many :hashtaggings, as: :hashtaggable, class_name: "SimpleHashtag::Hashtagging", dependent: :destroy
has_many :hashtags, through: :hashtaggings, class_name: "SimpleHashtag::Hashtag"
Expand All @@ -15,9 +17,23 @@ def hashtaggable_content
end

def update_hashtags
if hashtag_guard.present?
if hashtag_guard.respond_to?(:call)
return unless hashtag_guard.call self
elsif self.respond_to? hashtag_guard
return unless self.send hashtag_guard
else
raise InvalidHashtagGuard
end
end

self.hashtags = parsed_hashtags
end

def hashtag_guard
self.class.hashtag_guard
end

def parsed_hashtags
parsed_hashtags = []
array_of_hashtags_as_string = scan_for_hashtags(hashtaggable_content)
Expand All @@ -36,10 +52,15 @@ def scan_for_hashtags(content)

module ClassMethods
attr_accessor :hashtaggable_attribute_name
attr_accessor :hashtag_guard

def hashtaggable_attribute(name=nil)
self.hashtaggable_attribute_name ||= name || :body
end

def hashtag_if(guard)
self.hashtag_guard = guard
end
end
end
end
49 changes: 49 additions & 0 deletions spec/simple_hashtag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,54 @@
h.name.should eq "chance"
h.hashtaggables.count.should eq 2
end

context "with a lamdba guard" do
class GuardedPost < Post
hashtag_if lambda { |post| post.body =~ /Community/ }
end

it "creates hashtags if the guard is fulfilled" do
GuardedPost.create(body: 'Community saved! #sixseasonsandamovie')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#sixseasonsandamovie

p = Post.last
p.hashtags.first.name.should eq 'sixseasonsandamovie'
end

it "does not create hashtags if the guard is notfulfilled" do
GuardedPost.create(body: 'To boldly go... #startrek')
p = Post.last
p.hashtags.should == []
end
end

context "with a method guard" do
class MethodGuardedPost < Post
hashtag_if :is_community?

def is_community?
body =~ /Community/
end
end

it "creates hashtags if the guard is fulfilled" do
MethodGuardedPost.create(body: 'Community saved! #sixseasonsandamovie')
Post.last.hashtags.first.name.should eq 'sixseasonsandamovie'
end

it "does not create hashtags if the guard is notfulfilled" do
MethodGuardedPost.create(body: 'To boldly go... #startrek')
Post.last.hashtags.should == []
end
end

context "with an invalid guard" do
class PoorlyGuardedPost < Post
hashtag_if :this_isnt_even_real
end
end

it "raises an error" do
p = PoorlyGuardedPost.new(body: '#doesntmatter #thisisgoingtofail')
expect{ p.save! }.to raise_error SimpleHashtag::Hashtaggable::InvalidHashtagGuard
end
end
end