-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
238 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
class ApplicationController < ActionController::Base | ||
include Authentication | ||
|
||
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found | ||
|
||
private | ||
|
||
def record_not_found | ||
render file: Rails.public_path.join("404.html").to_s, status: :not_found, layout: false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class ProfilesController < ApplicationController | ||
include ProfileHelper | ||
|
||
before_action :set_profile, only: [:show, :qr_code] | ||
|
||
def show | ||
end | ||
|
||
def qr_code | ||
send_data( | ||
qr_code_png(@profile), | ||
filename: "#{@profile.name}.png", | ||
type: "image/png", | ||
disposition: "attachment" | ||
) | ||
end | ||
|
||
private | ||
|
||
def set_profile | ||
@profile = Profile.public_profiles.find_by!(uuid: params[:uuid]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ProfileHelper | ||
def qr_code_svg(profile) | ||
QRCodeGenerator.new(profile_url(profile.uuid)).as_svg(module_size: 4) | ||
end | ||
|
||
def qr_code_png(profile) | ||
QRCodeGenerator.new(profile_url(profile.uuid)).as_png(size: 240) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
|
||
export default class extends Controller { | ||
static targets = ['button'] | ||
|
||
async share () { | ||
const button = this.buttonTarget | ||
|
||
if (navigator.share) { | ||
try { | ||
const file = await fetch(button.dataset.url) | ||
.then((response) => response.blob()) | ||
.then((blob) => new File([blob], 'qr.png', { type: 'image/png' })) | ||
|
||
await navigator.share({ | ||
files: [file], title: 'QR Code' | ||
}) | ||
} catch (err) { | ||
window.open(button.dataset.url) | ||
} | ||
} else { | ||
window.open(button.dataset.url) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class QRCodeGenerator | ||
def initialize(url) | ||
@url = url | ||
end | ||
|
||
def as_svg(options = {}) | ||
qr.as_svg(options) | ||
end | ||
|
||
def as_png(options = {}) | ||
qr.as_png(options) | ||
end | ||
|
||
private | ||
|
||
attr_reader :url | ||
|
||
def qr | ||
@_qr ||= RQRCode::QRCode.new(url) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<h1>Profile</h1> | ||
|
||
<h2>My QR Code</h2> | ||
|
||
<%= render inline: qr_code_svg(@profile) %> | ||
<br> | ||
|
||
<!-- Save QR --> | ||
<%= link_to 'Save QR Code', qr_code_profile_path(@profile.uuid), method: :get %> | ||
<br> | ||
<br> | ||
|
||
<!-- Share QR --> | ||
<div data-controller="qr"> | ||
<button id="shareQR" data-action="click->qr#share" data-qr-target="button" data-url="<%= qr_code_profile_path(@profile.uuid) %>"> | ||
Share | ||
</button> | ||
</div> | ||
<br> | ||
|
||
<!-- Open Camera --> | ||
<h3>Scan a code</h3> | ||
<input type="file" accept="image/*" capture="capture"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddProfileUuidToProfile < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :profiles, :uuid, :string | ||
add_index :profiles, :uuid, unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe ProfilesController, type: :controller do | ||
let(:profile) { create(:profile, :with_user, :public) } | ||
|
||
before do | ||
sign_in(profile.user) | ||
end | ||
|
||
describe "GET #show" do | ||
it "returns a success response" do | ||
get :show, params: {uuid: profile.uuid} | ||
expect(response).to have_http_status(:success) | ||
end | ||
|
||
context "when the profile is not public" do | ||
let(:profile) { create(:profile, :with_user, is_public: false) } | ||
|
||
it "returns a not found response" do | ||
get :show, params: {uuid: profile.uuid} | ||
expect(response).to have_http_status(:not_found) | ||
end | ||
end | ||
end | ||
|
||
describe "GET #qr_code" do | ||
it "returns a PNG" do | ||
get :qr_code, params: {uuid: profile.uuid} | ||
expect(response.headers["Content-Type"]).to eq("image/png") | ||
expect(response.headers["Content-Disposition"]).to include("#{profile.name}.png") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe ProfileHelper, type: :helper do | ||
describe "#qr_code_svg" do | ||
let(:profile) { create(:profile, :with_user) } | ||
|
||
it "generates a QR code SVG" do | ||
expect(helper.qr_code_svg(profile)).to include("<svg") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe QRCodeGenerator do | ||
let(:qr_code_generator) { described_class.new(Faker::Internet.url) } | ||
|
||
describe "#as_svg" do | ||
it "returns an SVG" do | ||
expect(qr_code_generator.as_svg).to include("<svg") | ||
end | ||
end | ||
|
||
describe "#as_png" do | ||
it "returns a PNG" do | ||
expect(qr_code_generator.as_png.to_s).to include("PNG") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters