Skip to content

Commit

Permalink
Top label (#13)
Browse files Browse the repository at this point in the history
* Added feature of top label

* added feature to readme

* Fix rubocop warnings

* Update readme
  • Loading branch information
dannnylo authored Jun 16, 2024
1 parent 854aab4 commit 805e719
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ Sidekiq::Belt.configure do |config|
end
```

### Add to your web sidekiq a top label by enviroment (sidekiq)

This feature adds a little line on top of Sidekiq web that shows a configurable message.

![Top Page Development](https://github.com/dannnylo/sidekiq-belt/assets/20794/b1e2f6c2-a257-4172-92ec-09c61511334b)
![Top Page Production](https://github.com/dannnylo/sidekiq-belt/assets/20794/8e64d0e8-dcb2-42ee-b184-67d2f0b2cf6f)

To enable this feature, pass the `top_label` option:
```ruby
Sidekiq::Belt.use!([:top_label])
```

```ruby
Sidekiq::Belt.configure do |config|
config.top_label = {
production: {
background_color: 'red',
text: 'Be carefull',
color: 'white'
},
development: {
background_color: 'green',
text: 'You are safe!',
color: 'white'
}
}
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
10 changes: 9 additions & 1 deletion lib/sidekiq/belt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ def self.configure
end

def self.config
@config ||= Struct.new(:run_jobs).new([])
@config ||= Struct.new(:run_jobs, :top_label).new([], {})
end

def self.env
(Sidekiq.default_configuration[:environment] ||
ENV["APP_ENV"] ||
ENV["RAILS_ENV"] ||
ENV["RACK_ENV"] ||
"development").to_sym
end
end
end
2 changes: 2 additions & 0 deletions lib/sidekiq/belt/community/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
require "sidekiq"

require_relative "run_job"
require_relative "top_label"

module Sidekiq
module Belt
module Community
module Files
def self.use!(options = [:all])
Sidekiq::Belt::Community::RunJob.use! if should_use?(:run_job, options)
Sidekiq::Belt::Community::TopLabel.use! if should_use?(:top_label, options)

true
end
Expand Down
29 changes: 29 additions & 0 deletions lib/sidekiq/belt/community/top_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "sidekiq/web"
require "sidekiq/web/helpers"

module Sidekiq
module Belt
module Community
module TopLabel
def self.use!
Sidekiq::WebActionHelper.change_layout do |content|
top_label = (Sidekiq::Belt.config.top_label || {}).fetch(Sidekiq::Belt.env, {})

html = "<div class='container-fluid'
style='background: #{::Rack::Utils.escape_html(top_label.fetch(:background_color, "red"))};
text-align: center;
color: #{::Rack::Utils.escape_html(top_label.fetch(:color, "white"))};'>
&nbsp;#{::Rack::Utils.escape_html(top_label[:text].to_s)}&nbsp;
</div>"
unless top_label.empty?
content.gsub!('<div class="container-fluid">',
"#{html} <div class='container-fluid'>")
end
end
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/sidekiq/web_action_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ def render(engine, content, options = {})

super(engine, content, options)
end

def self.change_layout(&block)
Sidekiq::Config::DEFAULTS[:layout_changes] ||= []
Sidekiq::Config::DEFAULTS[:layout_changes] << block
end

def _render
content = super

layout_changes = Sidekiq::Config::DEFAULTS[:layout_changes] || []

layout_changes.each do |content_block|
content_block.call(content)
end

content
end
end

Sidekiq::WebAction.prepend(Sidekiq::WebActionHelper)
Expand Down

0 comments on commit 805e719

Please sign in to comment.