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

Middlewares #2

Merged
merged 5 commits into from Feb 7, 2019
Merged

Middlewares #2

merged 5 commits into from Feb 7, 2019

Conversation

nesaulov
Copy link
Member

Consider having a common logic that has to be executed in many services inside an application. Let's say we have an authorization process:

class UserDependentWebAction < Polist::Service
  def call
    fail!(code: :unauthorized) unless user.admin?
    # ...business logic
  end
  
  private

  def user
    User.find(id: params[:user_id])
  end
end

class AnotherUserDependentWebAction < Polist::Service
  def call
    fail!(code: :unauthorized) unless user.admin?
    # ...business logic
  end

  private

  def user
    User.find(id: params[:user_id])
  end
end

Wouldn't it be better to have middlewares?

class UserAuthorizationMiddleware < Polist::Service::Middleware
  def call
    fail!(code: :unauthorized) unless user.admin?
  end

  private

  def user
    User.find(id: service.params[:user_id])
  end
end

class UserDependentWebAction < Polist::Service
  register_middleware UserAuthorizationMiddleware

  def call
    # ...business logic
  end
end

class AnotherUserDependentWebAction < Polist::Service
  register_middleware UserAuthorizationMiddleware

  def call
    # ...business logic
  end
end

@tycooon what do you think?

@nesaulov nesaulov requested a review from tycooon October 20, 2018 14:43
@nesaulov
Copy link
Member Author

Wow, I did not notice #1. If your opinion on this topic has not changed, I can make it a plugin system instead of what is present in this pull request.

@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@umbrellio umbrellio deleted a comment from houndci-bot Oct 22, 2018
@@ -52,6 +71,7 @@ def initialize(params = {})
def call; end

def run
call_middlewares
Copy link
Member

Choose a reason for hiding this comment

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

This way the middlewares are only going to be called if I do Example.run and not if I do Example.call. And the only difference between these 2 methods should be that the latter raises exception in case of failure.

@tycooon
Copy link
Member

tycooon commented Oct 22, 2018

I think that this feature is useful enough to be in core gem, but I see some problems with the implementation.

Copy link
Member

@tycooon tycooon left a comment

Choose a reason for hiding this comment

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

🤔

@JelF
Copy link

JelF commented Oct 25, 2018

I think that this feature is useful enough to be in core gem, but I see some problems with the implementation.

Good news that we already have callbacks implementation in https://github.com/umbrellio/verifly (Verifly::DependentCallbacks)

around callback is same thing as middleware

@coveralls
Copy link

Pull Request Test Coverage Report for Build 24

  • 91 of 91 (100.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 22: 0.0%
Covered Lines: 305
Relevant Lines: 305

💛 - Coveralls

@coveralls
Copy link

coveralls commented Feb 4, 2019

Pull Request Test Coverage Report for Build 25

  • 91 of 91 (100.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 22: 0.0%
Covered Lines: 305
Relevant Lines: 305

💛 - Coveralls

@nesaulov
Copy link
Member Author

nesaulov commented Feb 4, 2019

We can either wait for rubocop/rubocop#6711 to be released (and rubocop-config-umbrellio to be updated), or I can disable this cop manually for this gem. @tycooon

@tycooon
Copy link
Member

tycooon commented Feb 4, 2019

Let’s just disable it for now.

@tycooon tycooon merged commit e714e12 into umbrellio:master Feb 7, 2019
tycooon added a commit that referenced this pull request Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants