Skip to content

Commit

Permalink
Merge pull request #4132 from avalonmediasystem/safe_constantize
Browse files Browse the repository at this point in the history
Simplify and avoid error handling by using safe_constantize
  • Loading branch information
phuongdh authored May 18, 2020
2 parents b694325 + be17b9b commit 2be1b6a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 2 additions & 3 deletions app/models/master_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,8 @@ def update_ingest_batch
end

def find_encoder_class(klass_name)
return nil if klass_name.blank? || !Object.const_defined?(klass_name)
klass = Object.const_get(klass_name)
return klass if klass.ancestors.include?(ActiveEncode::Base)
klass = klass_name&.safe_constantize
klass if klass&.ancestors&.include?(ActiveEncode::Base)
end

def stop_processing!
Expand Down
5 changes: 2 additions & 3 deletions app/presenters/speedy_af/proxy/master_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ def encoder_class
end

def find_encoder_class(klass_name)
return nil if klass_name.blank? || !Object.const_defined?(klass_name)
klass = Object.const_get(klass_name)
return klass if klass.ancestors.include?(ActiveEncode::Base)
klass = klass_name&.safe_constantize
klass if klass&.ancestors&.include?(ActiveEncode::Base)
end

def display_title
Expand Down
5 changes: 5 additions & 0 deletions spec/models/master_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@
expect(subject.encoder_class).to eq(WatchedEncode)
end

it "should fall back to Watched when a workflow class can't be resolved" do
subject.encoder_classname = 'my-awesomeEncode'
expect(subject.encoder_class).to eq(WatchedEncode)
end

it "should resolve an explicitly named encoder class" do
stub_const("EncoderModule::MyEncoder", Class.new(ActiveEncode::Base))
subject.encoder_classname = 'EncoderModule::MyEncoder'
Expand Down
7 changes: 7 additions & 0 deletions spec/presenters/speedy_af/proxy/master_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
end
end

context 'with invalid class name' do
let(:master_file) { FactoryBot.create(:master_file, encoder_classname: 'my-awesomeEncode') }
it "should fall back to Watched when a workflow class can't be resolved" do
expect(subject.encoder_class).to eq(WatchedEncode)
end
end

context 'with encoder class name' do
let(:master_file) { FactoryBot.create(:master_file, encoder_classname: 'EncoderModule::MyEncoder') }
it "should resolve an explicitly named encoder class" do
Expand Down

0 comments on commit 2be1b6a

Please sign in to comment.