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

Method Wrapping Exceptions #67

Merged
merged 7 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,8 @@ Tests are written using [RSpec](http://rspec.info/) and are setup to use [Apprai
$ bundle exec appraisal rspec spec/path/to/spec.rb

To run just a particular rails version:
$ bundle exec appraisal rails-5.1 rspec
$ bundle exec appraisal rails-5.2 rspec
$ bundle exec appraisal rails-6.0 rspec
$ bundle exec appraisal rails_6.1 rspec
$ bundle exec appraisal rails-7.0 rspec

### Console

Expand Down
2 changes: 1 addition & 1 deletion bin/ssh_to_container
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ if [[ -z $CONTAINER ]]; then
CONTAINER='ruby3'
fi

docker-compose run $CONTAINER sh
docker-compose run $CONTAINER bash
5 changes: 5 additions & 0 deletions lib/phi_attrs/phi_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ def set_all_phi_context_logged
# # through access logging.
#
def phi_wrap_method(method_name)
unless self.respond_to?(method_name)
PhiAttrs::Logger.warn("#{self.class.name} tried to wrap non-existant method (#{method_name})")
return
end
return if self.class.__phi_methods_wrapped.include? method_name

wrapped_method = :"__#{method_name}_phi_wrapped"
Expand Down Expand Up @@ -659,6 +663,7 @@ def phi_wrap_method(method_name)
# @private
#
def phi_wrap_extension(method_name)
raise NameError, "Undefined relationship in `extend_phi_access`: #{method_name}" unless self.respond_to?(method_name)
return if self.class.__phi_methods_to_extend.include? method_name

wrapped_method = wrapped_extended_name(method_name)
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/models/missing_attribute_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class MissingAttributeModel < ApplicationRecord
phi_model
phi_include_methods :non_existent_method
end
6 changes: 6 additions & 0 deletions spec/dummy/app/models/missing_extend_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class MissingExtendModel < ApplicationRecord
phi_model
extend_phi_access :non_existent_model
end
8 changes: 8 additions & 0 deletions spec/dummy/db/migrate/20170214100255_create_patient_infos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,13 @@ def change
t.belongs_to :patient_info
t.string :data
end

create_table :missing_attribute_model do |t|
t.timestamps
end

create_table :missing_extend_model do |t|
t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
t.index ["patient_info_id"], name: "index_health_records_on_patient_info_id"
end

create_table "missing_attribute_model", force: :cascade do |t|
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
end

create_table "missing_extend_model", force: :cascade do |t|
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
end

create_table "patient_details", force: :cascade do |t|
t.integer "patient_info_id"
t.string "detail"
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/factories/missing_attribute_models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

FactoryBot.define do
factory :missing_attribute_model do
end
end
6 changes: 6 additions & 0 deletions spec/dummy/factories/missing_extend_models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

FactoryBot.define do
factory :missing_extend_model do
end
end
20 changes: 20 additions & 0 deletions spec/phi_attrs/phi_record/phi_wrapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'phi_wrapping' do
file_name = __FILE__

let(:missing_attribute_model) { build(:missing_attribute_model) }
let(:missing_extend_model) { build(:missing_extend_model) }

context 'non existant attributes' do
it 'wrapping a method' do |t|
expect{missing_attribute_model}.not_to raise_error
end

it 'extending a model' do |t|
expect{missing_extend_model}.to raise_error(NameError)
end
end
end