Skip to content

Commit

Permalink
Merge pull request #22578 from kbrock/lenient_supported
Browse files Browse the repository at this point in the history
Allow `supports?(feature)` to work without requiring `supports_not`
  • Loading branch information
agrare committed Jul 11, 2023
2 parents c37f643 + 72a13ee commit d9924a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 15 additions & 3 deletions app/models/mixins/supports_feature_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ def unsupported_reason(feature)

# query the instance if the feature is supported or not
def supports?(feature)
public_send("supports_#{feature}?")
method_name = "supports_#{feature}?"
if respond_to?(method_name)
public_send(method_name)
else
unsupported_reason_add(feature)
false
end
end

private
Expand Down Expand Up @@ -108,7 +114,13 @@ def supports_not(feature, reason: nil)

# query the class if the feature is supported or not
def supports?(feature)
public_send("supports_#{feature}?")
method_name = "supports_#{feature}?"
if respond_to?(method_name)
public_send(method_name)
else
unsupported_reason_add(feature)
false
end
end

# all subclasses that are considered for supporting features
Expand Down Expand Up @@ -147,7 +159,7 @@ def providers_supporting(feature)
# query the class for the reason why something is unsupported
def unsupported_reason(feature)
feature = feature.to_sym
public_send("supports_#{feature}?") unless unsupported.key?(feature)
supports?(feature) unless unsupported.key?(feature)
unsupported[feature]
end

Expand Down
22 changes: 19 additions & 3 deletions spec/models/mixins/supports_feature_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def initialize(values = {})

describe ".supports_feature?" do
it "defines supports on the class" do
expect(test_class.supports?(:std_accept)).to be_truthy
expect(test_class.supports?(:module_accept)).to be_truthy
expect(test_class.supports?(:std_denial)).to be_falsey
expect(test_class.supports_std_accept?).to be_truthy
expect(test_class.supports_module_accept?).to be_truthy
expect(test_class.supports_std_denial?).to be_falsey
end
end

Expand All @@ -66,6 +66,10 @@ def initialize(values = {})
expect(test_class.supports?(:dynamic_feature)).to be_truthy
end

it "handles unknown supports" do
expect(test_class.supports?(:denial_unknown_feature)).to be_falsey
end

context "with child class" do
it "overrides to deny" do
child_class = define_model(nil, test_class, :std_accept => false, :module_accept => false, :dynamic_feature => false)
Expand Down Expand Up @@ -99,6 +103,10 @@ def initialize(values = {})
expect(test_inst.supports?(:denial_no_reason)).to be_falsey
end

it "handles unknown supports" do
expect(test_inst.supports?(:denial_unknown_feature)).to be_falsey
end

it "denies dynamic attrs" do
test_inst = test_class.new(:attr1 => false)

Expand Down Expand Up @@ -183,6 +191,10 @@ def initialize(values = {})
test_class.supports_not :denial_no_reason
expect(test_class.unsupported_reason(:denial_no_reason)).to eq("Feature not available/supported")
end

it "defaults denial reason for unknown feature" do
expect(test_class.unsupported_reason(:denial_unknown_feature)).to eq("Feature not available/supported")
end
end

describe '#unsupported_reason' do
Expand All @@ -197,6 +209,10 @@ def initialize(values = {})
expect(test_inst.unsupported_reason(:denial_no_reason)).to eq("Feature not available/supported")
end

it "defaults denial reason for unknown feature" do
expect(test_inst.unsupported_reason(:denial_unknown_feature)).to eq("Feature not available/supported")
end

it "gives reason when dynamic feature" do
test_inst = test_class.new(:attr1 => false)

Expand Down

0 comments on commit d9924a7

Please sign in to comment.