Replace dry-struct with ActiveModel (#9534)

This commit is contained in:
Michael Kohl 2020-07-27 22:42:37 +07:00 committed by GitHub
parent 0c8af6bd1e
commit 57359091a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 40 additions and 44 deletions

View file

@ -33,7 +33,6 @@ gem "devise", "~> 4.7" # Flexible authentication solution for Rails
gem "devise_invitable", "~> 2.0.0" # Allows invitations to be sent for joining
gem "dogstatsd-ruby", "~> 4.8" # A client for DogStatsD, an extension of the StatsD metric server for Datadog
gem "doorkeeper", "~> 5.4" # Oauth 2 provider
gem "dry-struct", "~> 1.2" # Typed structs and value objects
gem "elasticsearch", "~> 7.8" # Powers DEVs core search functionality
gem "email_validator", "~> 2.0" # Email validator for Rails and ActiveModel
gem "emoji_regex", "~> 3.0" # A pair of Ruby regular expressions for matching Unicode Emoji symbols

View file

@ -262,33 +262,6 @@ GEM
unf (>= 0.0.5, < 1.0.0)
doorkeeper (5.4.0)
railties (>= 5)
dry-configurable (0.11.1)
concurrent-ruby (~> 1.0)
dry-core (~> 0.4, >= 0.4.7)
dry-equalizer (~> 0.2)
dry-container (0.7.2)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.4.9)
concurrent-ruby (~> 1.0)
dry-equalizer (0.3.0)
dry-inflector (0.2.0)
dry-logic (1.0.6)
concurrent-ruby (~> 1.0)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-struct (1.3.0)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.3)
dry-types (~> 1.3)
ice_nine (~> 0.11)
dry-types (1.3.1)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.3)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.0, >= 1.0.2)
elasticsearch (7.8.0)
elasticsearch-api (= 7.8.0)
elasticsearch-transport (= 7.8.0)
@ -917,7 +890,6 @@ DEPENDENCIES
devise_invitable (~> 2.0.0)
dogstatsd-ruby (~> 4.8)
doorkeeper (~> 5.4)
dry-struct (~> 1.2)
elasticsearch (~> 7.8)
email_validator (~> 2.0)
emoji_regex (~> 3.0)

View file

@ -1,13 +1,21 @@
module Notifications
module NewFollower
module Types
include Dry.Types
end
class FollowData
class DataError < RuntimeError; end
class FollowData < Dry::Struct
attribute :followable_id, Types::Strict::Integer
attribute :followable_type, Types::Strict::String.enum("User", "Organization")
attribute :follower_id, Types::Strict::Integer
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :followable_id, :followable_type, :follower_id
validates :followable_id, numericality: { only_integer: true }
validates :followable_type, inclusion: { in: %w[User Organization] }
validates :follower_id, numericality: { only_integer: true }
def initialize(*args, **kwargs)
super
raise DataError unless valid?
end
end
end
end

View file

@ -1,13 +1,21 @@
module Notifications
module Reactions
module Types
include Dry.Types
end
class ReactionData
class DataError < RuntimeError; end
class ReactionData < Dry::Struct
attribute :reactable_id, Types::Strict::Integer
attribute :reactable_type, Types::Strict::String.enum("Article", "Comment")
attribute :reactable_user_id, Types::Strict::Integer
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :reactable_id, :reactable_type, :reactable_user_id
validates :reactable_id, numericality: { only_integer: true }
validates :reactable_type, inclusion: { in: %w[Article Comment] }
validates :reactable_user_id, numericality: { only_integer: true }
def initialize(*args, **kwargs)
super
raise DataError unless valid?
end
end
end
end

View file

@ -22,7 +22,7 @@ RSpec.describe Notifications::NewFollower::Send, type: :service do
tag_follow = user.follow(tag)
expect do
described_class.call(follow_data(tag_follow))
end.to raise_error(Dry::Struct::Error)
end.to raise_error(Notifications::NewFollower::FollowData::DataError)
end
end

View file

@ -15,6 +15,15 @@ RSpec.describe Notifications::Reactions::Send, type: :service do
}
end
context "when data is invalid" do
it "raises an exception" do
invalid_data = reaction_data(article_reaction).except(:reactable_id)
expect do
described_class.call(invalid_data, user)
end.to raise_error(Notifications::Reactions::ReactionData::DataError)
end
end
context "when a reaction is ok" do
it "creates a notification" do
expect do

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.