Skip to content

Commit

Permalink
Create following functionality (#3)
Browse files Browse the repository at this point in the history
* Create following functionality

* Update readme
  • Loading branch information
nejdetkadir authored Oct 15, 2022
1 parent 91dd8c1 commit 889787c
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 43 deletions.
7 changes: 5 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ Style/Documentation:
Metrics/MethodLength:
Enabled: true
CountComments: false
Max: 20
Max: 30

Metrics/AbcSize:
Max: 25
Max: 30

Metrics/BlockLength:
Max: 30
102 changes: 91 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ gem 'followability', github: 'nejdetkadir/followability', branch: 'main'
```

Install the gem and add to the application's Gemfile by executing:

$ bundle add followability
```bash
$ bundle add followability
```

If bundler is not being used to manage dependencies, install the gem by executing:
```bash
$ gem install followability
```

$ gem install followability

Run the generator:

$ rails g followability:install
Run the generator for creating database migration and copying localization files.
```bash
$ rails g followability:install
```

## Usage
Simply drop in `followability` to a model:
Expand All @@ -47,7 +50,6 @@ Avaiable methods:
- following?
- mutual_following_with?
- sent_follow_request_to?
- follow_request_sent_by?

### Usage
```ruby
Expand All @@ -60,20 +62,26 @@ Avaiable methods:
@foo.sent_follow_request_to?(@bar)
# => true

@bar.follow_request_sent_by(@foo)
# => true

@bar.decline_follow_request_of(@foo)
# => true

@foo.remove_follow_request_for(@bar)
# => false

@foo.errors.full_messages
# => [...]

@foo.mutual_following_with?(@bar)
# => false

@foo.errors.full_messages
# => [...]

@bar.following?(@foo)
# => false

@foo.errors.full_messages
# => [...]
```

### Blocking actions
Expand All @@ -98,6 +106,78 @@ Avaiable methods:
# => true
```

### Common
Avaiable methods:
- myself?

### Usage
```ruby
class User < ActiveRecord::Base
followability

def on_request_sent(record)
unless myself?(record)
# Do something
end
end
end
```

### Relations
Avaiable methods:
- follow_requests
- pending_requests
- followerable_relationships
- followable_relationships
- followers
- following
- blocks

### Usage
```ruby
@foo.follow_requests
# => [#<Followability::Relationship ...>]

@foo.pending_requests
# => [#<Followability::Relationship ...>]

@foo.followerable_relationships
# => [#<Followability::Relationship ...>]

@foo.followable_relationships
# => [#<Followability::Relationship ...>]

@foo.followers
# => [#<User ...>]

@foo.following
# => [#<User ...>]

@foo.blocks
# => [#<User ...>]
```

## I18n
```yml
---
en:
followability:
errors:
block:
block_to:
blocked_by: 'You can not block to who blocked to you'
already_blocked: '%{klass} already blocked'
not_blocked_for_blocking: 'You can not unblock to %{klass} because was not blocked'
follow:
remove_follow_request_for:
empty_relation: 'You can not remove follow request of %{klass} because was not sent'
send_follow_request_to:
blocked_by: 'You can not send follow request to who blocked to you'
following: 'You are already following to %{klass}'
already_sent: 'You are already sent follow request'
blocked: 'You can not send follow request to blocked %{klass}'
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
15 changes: 13 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@
en:
followability:
errors:
already_blocked: '%{klass} already blocked'
not_blocked_for_blocking: 'You can not unblock to %{klass} because was not blocked'
block:
block_to:
blocked_by: 'You can not block to who blocked to you'
already_blocked: '%{klass} already blocked'
not_blocked_for_blocking: 'You can not unblock to %{klass} because was not blocked'
follow:
remove_follow_request_for:
empty_relation: 'You can not remove follow request of %{klass} because was not sent'
send_follow_request_to:
blocked_by: 'You can not send follow request to who blocked to you'
following: 'You are already following to %{klass}'
already_sent: 'You are already sent follow request'
blocked: 'You can not send follow request to blocked %{klass}'
1 change: 1 addition & 0 deletions lib/followability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module Followability
extend ActiveSupport::Autoload

require_relative 'followability/followable/associations'
require_relative 'followability/followable/callbacks'
require_relative 'followability/followable/actions/common'
require_relative 'followability/followable/actions/follow'
Expand Down
31 changes: 30 additions & 1 deletion lib/followability/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,51 @@ def followability?
false
end

# rubocop:disable Metrics/MethodLength
def followability
class_eval do
def self.followability?
true
end

has_many :followability_relationships,
has_many :followerable_relationships,
as: :followerable,
class_name: 'Followability::Relationship',
dependent: :destroy

has_many :followable_relationships,
as: :followable,
class_name: 'Followability::Relationship',
dependent: :destroy

has_many :followers,
-> { Followability::Relationship.following },
through: :followable_relationships,
source: :followerable,
class_name: name,
source_type: name

has_many :following,
-> { Followability::Relationship.following },
through: :followerable_relationships,
source: :followable,
class_name: name,
source_type: name

has_many :blocks,
-> { Followability::Relationship.blocked },
through: :followerable_relationships,
source: :followable,
class_name: name,
source_type: name
end

include Followability::Followable::Associations
include Followability::Followable::Callbacks
include Followability::Followable::Actions::Common
include Followability::Followable::Actions::Follow
include Followability::Followable::Actions::Block
end
# rubocop:enable Metrics/MethodLength
end
end
20 changes: 13 additions & 7 deletions lib/followability/followable/actions/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ module Followability
module Followable
module Actions
module Block
I18N_SCOPE = 'followability.errors'
I18N_SCOPE = 'followability.errors.block'

def block_to(record)
if blocked_by?(record)
errors.add(:base, I18n.t('block_to.blocked_by', scope: I18N_SCOPE, klass: record.class))

return false
end

if blocked?(record)
errors.add(:base, I18n.t(:already_blocked, scope: I18N_SCOPE, klass: record.class))
errors.add(:base, I18n.t('block.already_blocked', scope: I18N_SCOPE, klass: record.class))

return false
end

relation = followability_relationships.blocked.new(followable: record,
status: Followability::Relationship.statuses[:blocked])
relation = followerable_relationships.blocked.new(followable: record,
status: Followability::Relationship.statuses[:blocked])

if relation.save
run_callback(self, callback: :on_followable_blocked)
Expand All @@ -34,7 +40,7 @@ def unblock_to(record)
return false
end

relation = followability_relationships.blocked.find_by(followable: record)
relation = followerable_relationships.blocked.find_by(followable: record)

if relation.destroy
run_callback(self, callback: :on_followable_unblocked)
Expand All @@ -48,11 +54,11 @@ def unblock_to(record)
end

def blocked?(record)
followability_relationships.blocked.exists?(followable: record)
followerable_relationships.blocked.exists?(followable: record)
end

def blocked_by?(record)
record.followability_relationships.blocked.exists?(followable: self)
record.followerable_relationships.blocked.exists?(followable: self)
end
end
end
Expand Down
12 changes: 0 additions & 12 deletions lib/followability/followable/actions/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ module Followability
module Followable
module Actions
module Common
def related_with_as_followerable?(record)
followability_relationships.exists?(followable: record)
end

def related_with_as_followable?(record)
followability_relationships.exists?(followerable: record)
end

def related?(record)
related_with_as_followerable?(record) || related_with_as_followable?(record)
end

def myself?(record)
id == record.id
end
Expand Down
Loading

0 comments on commit 889787c

Please sign in to comment.