-
-
Save plainprogrammer/1b747223c67c008f6356a75842cd57a3 to your computer and use it in GitHub Desktop.
Applying Clean Architecture: Repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Repositories | |
class BusinessProfiles | |
def find business_id | |
data_model = ::BusinessProfile.find_by business_id: business_id | |
if data_model | |
fields = fields_for data_model | |
profile = Nav::Entities::BusinessProfile.new data_model.business_id | |
fields.each { |name, value| profile.send(:"#{name}=", value) } | |
else | |
profile = Nav::Entities::BusinessProfile.new 0 | |
profile.errors << "can not find Business Profile with ID #{business_id}" | |
end | |
profile | |
end | |
def update for_id:, fields: {} | |
ActiveRecord::Base.transaction do | |
profile = BusinessProfile.find_or_create_by! business_id: for_id | |
fields.each do |name, value| | |
Field.create! business_profile: profile, | |
name: name, | |
value: value, | |
reported_at: DateTime.now | |
end | |
end | |
find for_id | |
end | |
private | |
def fields_for data_model | |
# ... | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment