Skip to content

Commit

Permalink
[FS-13] Show QR code in app panel (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
beetlegius-jt authored Jan 23, 2025
1 parent 364bf5c commit 92aa5ae
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ gem "combined_time_select", "~> 2.0"
gem "devise", "~> 4.9"
gem "ice_cube", "~> 0.17.0"
gem "pundit", "~> 2.4"
gem "rqrcode", "~> 2.2", require: false
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
choice (0.2.0)
chunky_png (1.4.0)
combined_time_select (2.0.0)
rails (>= 5.0.0)
concurrent-ruby (1.3.5)
Expand Down Expand Up @@ -283,6 +284,10 @@ GEM
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.4.0)
rqrcode (2.2.0)
chunky_png (~> 1.0)
rqrcode_core (~> 1.0)
rqrcode_core (1.2.0)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
Expand Down Expand Up @@ -446,6 +451,7 @@ DEPENDENCIES
pundit (~> 2.4)
rails (~> 8.0.1)
rails-erd (~> 1.7)
rqrcode (~> 2.2)
rspec-rails (~> 7.1)
rubocop (~> 1.70)
rubocop-capybara (~> 2.21)
Expand Down
15 changes: 15 additions & 0 deletions app/lib/concerns/callable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Callable
extend ActiveSupport::Concern

class_methods do
def call(*, **, &)
new(*, **).call(&)
end
end

included do
def call
raise NotImplementedError
end
end
end
9 changes: 9 additions & 0 deletions app/views/app/customers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
<i class="bi bi-person-fill"></i>
<%= @customer.name %>
</h2>

<div class="row">
<div class="col-md-4">
<%= QrCode.call Current.customer.id.to_s %>
</div>

<div class="col-md-8">
</div>
</div>
16 changes: 16 additions & 0 deletions lib/qr_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "rqrcode"

class QrCode
include Callable

attr_reader :text, :offset

def initialize(text, offset: 30)
@text = text
@offset = offset
end

def call
RQRCode::QRCode.new(text).as_svg(offset:, viewbox: true).html_safe
end
end
19 changes: 19 additions & 0 deletions spec/lib/qr_code_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.describe QrCode, type: :model do
describe ".call" do
let(:text) { "whatever" }
let(:offset) { 100 }
let(:qr_code) { instance_double("RQRCode::QRCode") }
let(:result) { "some valid html" }

subject { described_class.call(text, offset:) }

before do
allow(RQRCode::QRCode).to receive(:new).with(text).and_return(qr_code)
allow(qr_code).to receive(:as_svg).with(offset:, viewbox: true).and_return(result)
end

it { is_expected.to eq(result) }
end
end

0 comments on commit 92aa5ae

Please sign in to comment.