Skip to content

Commit 4d9014b

Browse files
Refactor Pro license validation logic and update related tests
1 parent 8a639dc commit 4d9014b

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

lib/react_on_rails/helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ def load_pack_for_generated_component(react_component_name, render_options)
448448
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
449449

450450
def pro_warning_badge_if_needed(force_load)
451-
return "".html_safe if !force_load && !ReactOnRails.configuration.force_load
452-
return "".html_safe if ReactOnRails::Utils.react_on_rails_pro?
451+
return "".html_safe unless force_load
452+
return "".html_safe if ReactOnRails::Utils.react_on_rails_pro_licence_valid?
453453

454454
warning_message = "[REACT ON RAILS] The 'force_load' feature requires a React on Rails Pro license. " \
455455
"Please visit https://shakacode.com/react-on-rails-pro to learn more."

lib/react_on_rails/utils.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require "active_support/core_ext/string"
88

99
module ReactOnRails
10-
module Utils
10+
module Utils # rubocop:disable Metrics/ModuleLength
1111
TRUNCATION_FILLER = "\n... TRUNCATED #{
1212
Rainbow('To see the full output, set FULL_TEXT_ERRORS=true.').red
1313
} ...\n".freeze
@@ -213,6 +213,21 @@ def self.react_on_rails_pro_version
213213
end
214214
end
215215

216+
def self.react_on_rails_pro_licence_valid?
217+
return @react_on_rails_pro_licence_valid if defined?(@react_on_rails_pro_licence_valid)
218+
219+
@react_on_rails_pro_licence_valid = begin
220+
return false unless react_on_rails_pro?
221+
222+
# Maintain compatibility with legacy versions of React on Rails Pro:
223+
# Earlier releases did not require license validation, as they were distributed as private gems.
224+
# This check ensures that the method works correctly regardless of the installed version.
225+
return true unless ReactOnRailsPro::Utils.respond_to?(:licence_valid?)
226+
227+
ReactOnRailsPro::Utils.licence_valid?
228+
end
229+
end
230+
216231
def self.rsc_support_enabled?
217232
return false unless react_on_rails_pro?
218233

spec/dummy/spec/helpers/react_on_rails_helper_spec.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ class PlainReactOnRailsHelper
2323
}
2424

2525
allow(ReactOnRails::Utils).to receive_messages(
26-
react_on_rails_pro?: true,
27-
react_on_rails_pro_version: "4.0.0",
28-
rsc_support_enabled?: false
26+
react_on_rails_pro_licence_valid?: true
2927
)
3028
end
3129

@@ -388,7 +386,7 @@ def helper.append_javascript_pack_tag(name, **options)
388386
subject(:react_app) { react_component("App", props: props, force_load: true) }
389387

390388
before do
391-
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
389+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro_licence_valid?).and_return(false)
392390
end
393391

394392
it { is_expected.to include(badge_html_string) }
@@ -403,7 +401,7 @@ def helper.append_javascript_pack_tag(name, **options)
403401
subject(:react_app) { react_component("App", props: props) }
404402

405403
before do
406-
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
404+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro_licence_valid?).and_return(false)
407405
end
408406

409407
around do |example|
@@ -419,7 +417,7 @@ def helper.append_javascript_pack_tag(name, **options)
419417
subject(:react_app) { react_component("App", props: props, force_load: false) }
420418

421419
before do
422-
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
420+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro_licence_valid?).and_return(false)
423421
end
424422

425423
it { is_expected.not_to include(badge_html_string) }
@@ -435,8 +433,7 @@ def helper.append_javascript_pack_tag(name, **options)
435433

436434
before do
437435
allow(ReactOnRails::Utils).to receive_messages(
438-
react_on_rails_pro?: true,
439-
react_on_rails_pro_version: "4.0.0"
436+
react_on_rails_pro_licence_valid?: true
440437
)
441438
end
442439

@@ -482,7 +479,7 @@ def helper.append_javascript_pack_tag(name, **options)
482479
subject(:react_app) { react_component_hash("App", props: props, force_load: true) }
483480

484481
before do
485-
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
482+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro_licence_valid?).and_return(false)
486483
end
487484

488485
it "adds badge to componentHtml" do
@@ -495,8 +492,7 @@ def helper.append_javascript_pack_tag(name, **options)
495492

496493
before do
497494
allow(ReactOnRails::Utils).to receive_messages(
498-
react_on_rails_pro?: true,
499-
react_on_rails_pro_version: "4.0.0"
495+
react_on_rails_pro_licence_valid?: true
500496
)
501497
end
502498

@@ -541,7 +537,7 @@ def helper.append_javascript_pack_tag(name, **options)
541537
subject(:store) { redux_store("reduxStore", props: props, force_load: true) }
542538

543539
before do
544-
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
540+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro_licence_valid?).and_return(false)
545541
end
546542

547543
it { is_expected.to include(badge_html_string) }
@@ -556,7 +552,7 @@ def helper.append_javascript_pack_tag(name, **options)
556552
subject(:store) { redux_store("reduxStore", props: props, force_load: false) }
557553

558554
before do
559-
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
555+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro_licence_valid?).and_return(false)
560556
end
561557

562558
it { is_expected.not_to include(badge_html_string) }
@@ -567,8 +563,7 @@ def helper.append_javascript_pack_tag(name, **options)
567563

568564
before do
569565
allow(ReactOnRails::Utils).to receive_messages(
570-
react_on_rails_pro?: true,
571-
react_on_rails_pro_version: "4.0.0"
566+
react_on_rails_pro_licence_valid?: true
572567
)
573568
end
574569

0 commit comments

Comments
 (0)