Skip to content

Commit cb670e7

Browse files
committed
Fix styles
1 parent a4c423a commit cb670e7

File tree

8 files changed

+166328
-121
lines changed

8 files changed

+166328
-121
lines changed

app/helpers/rails_pulse/application_helper.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,25 @@ def rails_pulse_icon(name, options = {})
1616
width = options[:width] || options["width"] || 24
1717
height = options[:height] || options["height"] || 24
1818
css_class = options[:class] || options["class"] || ""
19+
custom_style = options[:style] || options["style"]
20+
21+
# Normalize numeric width/height values into px for layout stability
22+
width_css = normalize_dimension(width)
23+
height_css = normalize_dimension(height)
24+
25+
default_style = [
26+
"display:inline-flex",
27+
"align-items:center",
28+
"justify-content:center",
29+
"width:#{width_css}",
30+
"height:#{height_css}",
31+
"flex-shrink:0"
32+
].join(";")
33+
34+
style_attribute = [default_style, custom_style].compact.join(";")
1935

2036
# Additional HTML attributes
21-
attrs = options.except(:width, :height, :class, "width", "height", "class")
37+
attrs = options.except(:width, :height, :class, :style, "width", "height", "class", "style")
2238

2339
content_tag("rails-pulse-icon",
2440
"",
@@ -29,13 +45,29 @@ def rails_pulse_icon(name, options = {})
2945
'rails-pulse--icon-height-value': height
3046
},
3147
class: css_class,
48+
style: style_attribute,
3249
**attrs
3350
)
3451
end
3552

3653
# Backward compatibility alias - can be removed after migration
3754
alias_method :lucide_icon, :rails_pulse_icon
3855

56+
def normalize_dimension(value)
57+
string = value.to_s
58+
return string if string.empty?
59+
60+
if string.match?(/[a-z%]+\z/i)
61+
string
62+
else
63+
number = Float(string)
64+
formatted = number == number.to_i ? number.to_i.to_s : number.to_s
65+
"#{formatted}px"
66+
end
67+
end
68+
69+
private :normalize_dimension
70+
3971
# Make Rails Pulse routes available as rails_pulse in views
4072
def rails_pulse
4173
@rails_pulse_helper ||= RailsPulseHelper.new(self)

app/views/layouts/rails_pulse/application.html.erb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@
1010

1111
<%= yield :head %>
1212

13+
<script nonce="<%= rails_pulse_csp_nonce %>">
14+
(function() {
15+
var storageKey = 'color-scheme';
16+
var root = document.documentElement;
17+
var scheme = 'light';
18+
19+
try {
20+
var stored = localStorage.getItem(storageKey);
21+
if (stored === 'dark' || stored === 'light') {
22+
scheme = stored;
23+
} else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
24+
scheme = 'dark';
25+
}
26+
} catch (error) {
27+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
28+
scheme = 'dark';
29+
}
30+
}
31+
32+
root.setAttribute('data-color-scheme', scheme);
33+
})();
34+
</script>
35+
1336
<!-- Rails Pulse Pre-compiled Assets (CSP-safe) -->
1437
<%= stylesheet_link_tag rails_pulse.asset_path('rails-pulse.css'), 'data-turbo-track': 'reload' %>
1538
<%= javascript_include_tag rails_pulse.asset_path('rails-pulse-icons.js'), 'data-turbo-track': 'reload', defer: true, nonce: rails_pulse_csp_nonce %>

public/rails-pulse-assets/rails-pulse.css

Lines changed: 3489 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/rails-pulse-assets/rails-pulse.js

Lines changed: 162757 additions & 110 deletions
Large diffs are not rendered by default.

test/helpers/rails_pulse/application_helper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class RailsPulse::ApplicationHelperTest < ActionView::TestCase
1111
assert_includes html, "rails-pulse--icon-name-value=\"alert\""
1212
assert_includes html, "rails-pulse--icon-width-value=\"24\""
1313
assert_includes html, "rails-pulse--icon-height-value=\"24\""
14+
assert_includes html, "style=\"display:inline-flex;align-items:center;justify-content:center;width:24px;height:24px;flex-shrink:0\""
1415
end
1516

1617
test "rails_pulse_icon applies custom width, height and class" do
@@ -19,6 +20,13 @@ class RailsPulse::ApplicationHelperTest < ActionView::TestCase
1920
assert_includes html, "rails-pulse--icon-width-value=\"32\""
2021
assert_includes html, "rails-pulse--icon-height-value=\"32\""
2122
assert_includes html, "class=\"my-class\""
23+
assert_includes html, "width:32px;height:32px"
24+
end
25+
26+
test "rails_pulse_icon merges custom style with default dimensions" do
27+
html = rails_pulse_icon("alert", style: "margin-left:4px")
28+
29+
assert_includes html, "flex-shrink:0;margin-left:4px"
2230
end
2331

2432
test "rails_pulse_icon passes through extra attributes" do

test/models/rails_pulse/job_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class JobTest < ActiveSupport::TestCase
2424
job = Job.create!(name: "RailsPulse::TestJob", queue_name: "default")
2525
run = job.runs.create!(run_id: "test-run-1", status: "running", occurred_at: Time.current, attempts: 0)
2626

27-
run.update!(status: "retried", duration: 200.0)
27+
run.update_columns(status: "retried", duration: 200.0)
2828

29-
job.apply_run!(run)
29+
job.apply_run!(run.reload)
3030
job.reload
3131

3232
assert_in_delta 200.0, job.avg_duration, 0.01

test/models/rails_pulse/summary_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class RailsPulse::SummaryTest < ActiveSupport::TestCase
99
assert belong_to(:summarizable).optional.matches?(RailsPulse::Summary.new)
1010
assert belong_to(:route).optional.matches?(RailsPulse::Summary.new)
1111
assert belong_to(:query).optional.matches?(RailsPulse::Summary.new)
12+
assert belong_to(:job).optional.matches?(RailsPulse::Summary.new)
1213
end
1314

1415
# Test validations
@@ -47,7 +48,7 @@ class RailsPulse::SummaryTest < ActiveSupport::TestCase
4748
end
4849

4950
test "should include ransackable associations" do
50-
expected_associations = %w[route query]
51+
expected_associations = %w[job query route]
5152

5253
assert_equal expected_associations.sort, RailsPulse::Summary.ransackable_associations.sort
5354
end

test/services/rails_pulse/job_run_collector_test.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize(job_id: SecureRandom.uuid, queue_name: "default", executions: 0)
1414
end
1515

1616
def self.name
17-
"RailsPulse::JobRunCollectorTest::FakeJob"
17+
"JobRunCollectorFakeJob"
1818
end
1919

2020
def arguments
@@ -82,7 +82,7 @@ def enqueued_at
8282
end
8383

8484
test "active job integration wraps perform now" do
85-
class InstrumentedJob < ActiveJob::Base
85+
klass = Class.new(ActiveJob::Base) do
8686
queue_as :default
8787

8888
def perform
@@ -92,21 +92,29 @@ def perform
9292
end
9393
end
9494

95-
assert_difference -> { RailsPulse::Job.where(name: "RailsPulse::JobRunCollectorTest::InstrumentedJob").count }, 1 do
95+
if Object.const_defined?(:JobRunCollectorTestInstrumentedJob)
96+
Object.send(:remove_const, :JobRunCollectorTestInstrumentedJob)
97+
end
98+
99+
Object.const_set(:JobRunCollectorTestInstrumentedJob, klass)
100+
101+
assert_difference -> { RailsPulse::Job.where(name: JobRunCollectorTestInstrumentedJob.name).count }, 1 do
96102
assert_difference -> { RailsPulse::JobRun.count }, 1 do
97-
InstrumentedJob.perform_now
103+
JobRunCollectorTestInstrumentedJob.perform_now
98104
end
99105
end
100106

101-
job = RailsPulse::Job.find_by(name: "RailsPulse::JobRunCollectorTest::InstrumentedJob")
107+
job = RailsPulse::Job.find_by(name: JobRunCollectorTestInstrumentedJob.name)
102108

103109
assert_not_nil job
104110
run = job.runs.order(:created_at).last
105111

106112
assert_equal "success", run.status
107113
assert_not_empty RailsPulse::Operation.where(job_run: run)
108114
ensure
109-
Object.send(:remove_const, :InstrumentedJob) if defined?(InstrumentedJob)
115+
if Object.const_defined?(:JobRunCollectorTestInstrumentedJob)
116+
Object.send(:remove_const, :JobRunCollectorTestInstrumentedJob)
117+
end
110118
end
111119
end
112120
end

0 commit comments

Comments
 (0)