Replace dry-struct with ActiveModel (#9534)
This commit is contained in:
parent
0c8af6bd1e
commit
57359091a9
14 changed files with 40 additions and 44 deletions
1
Gemfile
1
Gemfile
|
|
@ -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
|
||||
|
|
|
|||
28
Gemfile.lock
28
Gemfile.lock
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
BIN
vendor/cache/dry-configurable-0.11.1.gem
vendored
BIN
vendor/cache/dry-configurable-0.11.1.gem
vendored
Binary file not shown.
BIN
vendor/cache/dry-container-0.7.2.gem
vendored
BIN
vendor/cache/dry-container-0.7.2.gem
vendored
Binary file not shown.
BIN
vendor/cache/dry-core-0.4.9.gem
vendored
BIN
vendor/cache/dry-core-0.4.9.gem
vendored
Binary file not shown.
BIN
vendor/cache/dry-equalizer-0.3.0.gem
vendored
BIN
vendor/cache/dry-equalizer-0.3.0.gem
vendored
Binary file not shown.
BIN
vendor/cache/dry-inflector-0.2.0.gem
vendored
BIN
vendor/cache/dry-inflector-0.2.0.gem
vendored
Binary file not shown.
BIN
vendor/cache/dry-logic-1.0.6.gem
vendored
BIN
vendor/cache/dry-logic-1.0.6.gem
vendored
Binary file not shown.
BIN
vendor/cache/dry-struct-1.3.0.gem
vendored
BIN
vendor/cache/dry-struct-1.3.0.gem
vendored
Binary file not shown.
BIN
vendor/cache/dry-types-1.3.1.gem
vendored
BIN
vendor/cache/dry-types-1.3.1.gem
vendored
Binary file not shown.
Loading…
Add table
Reference in a new issue