Skip to content

Commit

Permalink
Fix rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
cintamani committed Oct 14, 2019
1 parent 51a89ae commit f690846
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module WasteCarriersEngine
# Define the attributes a registration or a renewal has
# rubocop:disable Metrics/ModuleLength
module CanHaveRegistrationAttributes
extend ActiveSupport::Concern
include Mongoid::Document
Expand All @@ -18,7 +17,7 @@ module CanHaveRegistrationAttributes
# for comments in some places, and putting them on the line above breaks
# the formatting we have in place.
# rubocop:disable Metrics/LineLength
embeds_many :addresses, class_name: "WasteCarriersEngine::Address"
embeds_many :addresses, class_name: "WasteCarriersEngine::Address"
reference_one :contact_address, collection: :addresses, find_by: { address_type: "POSTAL" }
reference_one :registered_address, collection: :addresses, find_by: { address_type: "REGISTERED" }

Expand Down Expand Up @@ -140,5 +139,4 @@ def declaration_confirmed?
end
# rubocop:enable Metrics/BlockLength
end
# rubocop:enable Metrics/ModuleLength
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,29 @@ def reference_one(attribute_name, collection:, find_by:)
end

define_method("#{attribute_name}=") do |new_object|
assign_attribute(attribute_name, collection, find_by, new_object)
assign_attribute(attribute_name, collection, new_object)
end
end
end

included do
def retrieve_attribute(attribute_name, collection, find_by)
instance_variable_get("@#{attribute_name}") ||
fetch_attribute(attribute_name, collection, find_by)
fetch_attribute(collection, find_by)
end

def assign_attribute(attribute_name, collection, find_by, new_object)
eval("#{attribute_name}.delete") if send(attribute_name)
eval("#{collection} << new_object")
# rubocop:disable Lint/UnusedMethodArgument
def assign_attribute(attribute_name, collection, new_object)
send(attribute_name)&.delete

instance_eval("#{collection} << new_object", __FILE__, __LINE__)

instance_variable_set("@#{attribute_name}", nil)
end
# rubocop:enable Lint/UnusedMethodArgument

def fetch_attribute(attribute_name, collection, find_by)
criteria = instance_eval("#{collection}.criteria")
def fetch_attribute(collection, find_by)
criteria = instance_eval("#{collection}.criteria", __FILE__, __LINE__)

criteria.where(find_by).first
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

RSpec.shared_examples "Can have registration attributes" do
include_examples "Can reference single document in collection",
Proc.new { create(:transient_registration, :has_required_data, :has_addresses) },
include_examples(
"Can reference single document in collection",
proc { create(:transient_registration, :has_required_data, :has_addresses) },
:contact_address,
Proc.new { subject.addresses.find_by(address_type: "POSTAL") },
proc { subject.addresses.find_by(address_type: "POSTAL") },
WasteCarriersEngine::Address.new,
:addresses
)

describe "#charity?" do
let(:transient_registration) { build(:transient_registration) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe ".reference_one" do
it "defines an attr getter for the given attribute" do
expect(subject).to respond_to("#{attribute}")
expect(subject).to respond_to(attribute.to_s)
end

it "defines an attr setter for the given attribute" do
Expand Down

0 comments on commit f690846

Please sign in to comment.