Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
An active record mention system developed using ruby on rails applying domain driven design and test driven development principles.
This gem is heavily influenced by cmer/socialization.
Add this line to your application's Gemfile:
gem 'mention_system'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mention_system
$ rails g mention_system
Let's suppose for a moment that you have a blog application and a Post can mention a User or several User models. The post model becomes the mentioner and the user model becomes the mentionee.
class User < ActiveRecord::Base
act_as_mentionee
validates :username, { presence: true, uniqueness: true }
end
class Post < ActiveRecord::Base
act_as_mentioner
validates :content, presence: true
end
user.is_mentionee? # returns true
user.mentioned_by?(post) # returns true if post mentions the user object, false otherwise
user.mentioners_by(Post) # returns a scope of MentionSystem::Mention join model that belongs to the user object and belongs to mentioner objects of type Post
post.is_mentioner? # returns true
post.mention(user) # Creates an instance of MentionSystem::Mention join model associating the post object and the user object, returns true if succeded, false otherwise
post.unmention(user) # Destroys an instance of MentionSystem::Mention join model that associates the post object and the user object, returns true if succeded, false otherwise
post.toggle_mention(user) # Mentions / unmentions the user
post.mentions?(user) # returns true if the post object mentions the user object, false otherwise
post.mentionees_by(User) # returns a scope of MentionSystem::Mention join model that belongs to the post object and belongs to mentionee objects of type User
Mention processors are objects in charge of computing mentions between mentioner objects and mentionee objects. The framework provides a base mention processor class that the developer should subclass. Mention processors provide after / before callbacks to hook custom behavior before and after a mention is computed.
Let's suppose we want to parse a post content and process mentions in the form of "@username". We first define a custom mention processor instructing how to compute a post content and how to retrieve a collection of users by the handles mentioned in the post content.
class CustomMentionProcessor < MentionSystem::MentionProcessor
###
# This method returns the content used to parse mentions from the mentioner object, in this case is post's content
###
def extract_mentioner_content(post)
post.content
end
###
# This method should return a collection (must respond to each) of mentionee objects for a given set of handles
# In our case will be a collection of user objects
###
def find_mentionees_by_handles(*handles)
User.where(username: handles)
end
end
When we call
user1 = User.create(username: "test1")
user2 = User.create(username: "test2")
post = Post.create(content: "Post content mentioning @test1 and @test2")
m = CustomMentionProcessor.new
m.process_mentions(post)
It will process a mention between "post" and "user1" and also a mention between "post" and "user2".
Suppose we want to validate if a user can be mentioned before a mention is processed, we can register a before callback in the mention processor. Now lets suppose we want to send a notification email when a mention is given to a user, we can register an after callback in the mention processor.
Lets see an example of both:
m = CustomMentionProcessor.new
m.add_before_callback Proc.new { |post, user| user.mentions_allowed? }
m.add_after_callback Proc.new { |post, user| UserMailer.notify_mention(post, user) }
m.process_mentions(post)
Now when the mention processor process the mentions in the post content, it should validate first if the user is allowed to mention and after the mention is processed an email sent notifying the mention.
You can register several callbacks and they will be called in order.
You can override the prefix used to parse mentions, it defaults to "@" but you can change to any prefix you like, for example "#", so that mention processors can recognize #test1 and #test2 instead of @user1 and @user2.
To do that you need to override the mention_prefix method in the mention processor.
class CustomMentionProcessor < MentionSystem::MentionProcessor
private:
###
# Defines the mention prefix used to parse mentions, defaults "@"
###
def mention_prefix
"#"
end
end
For more information read the api documentation.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that mention_system demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.