From 8b0ddff515cb92b4ace384d00bfc2a8254c7b03f Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Sat, 16 Apr 2022 15:21:07 -0500 Subject: [PATCH] Fixes and add label to tables --- .gitignore | 1 + Gemfile | 2 + Gemfile.lock | 9 +++++ app/controllers/dashboard_controller.rb | 4 +- .../suppliers/tables_controller.rb | 2 +- app/models/supplier.rb | 3 +- app/models/table.rb | 1 + config/couchdb.yml | 2 +- drb_counter/drb_counter.rb | 1 + drb_counter/rebuild-docker.sh | 8 +++- lib/rqrcode-rails3/qr_container.svg | 38 +++++++++---------- spec/controllers/dashboard_controller_spec.rb | 21 ++++++++++ .../suppliers/tables_controller_spec.rb | 22 +++++------ 13 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 spec/controllers/dashboard_controller_spec.rb diff --git a/.gitignore b/.gitignore index 42594b16..d0cc8576 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,4 @@ erl_crash.dump public/user /config/master.key +/public/*.pem diff --git a/Gemfile b/Gemfile index 4421004f..d8280bc4 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ gem 'slim-rails' # Gems used only for assets and not required #gem 'jsonapi-serializers', path: '/home/benjamin/development/rails/components/jsonapi-serializers' gem 'jsonapi-serializers', github: 'bterkuile/jsonapi-serializers' +gem 'ruby-handlebars' group :assets do gem 'bourbon' #, '4.0.2' #animation keyframes gem 'coffee-rails', '>= 5.0.0' @@ -133,6 +134,7 @@ group :test do gem 'timecop' gem 'turnip' gem 'webmock' + gem 'rails-controller-testing' #gem 'rb-fsevent', require: false #if RUBY_PLATFORM =~ /darwin/i end diff --git a/Gemfile.lock b/Gemfile.lock index f8bfd815..6b6903e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -322,6 +322,7 @@ GEM mime-types mimemagic (~> 0.3.0) terrapin (~> 0.6.0) + parslet (1.8.2) pickadate-rails (3.5.6.0) railties (>= 3.1.0) poltergeist (1.18.1) @@ -365,6 +366,10 @@ GEM bundler (>= 1.15.0) railties (= 6.1.4.4) sprockets-rails (>= 2.0.0) + rails-controller-testing (1.0.5) + actionpack (>= 5.0.1.rc1) + actionview (>= 5.0.1.rc1) + activesupport (>= 5.0.1.rc1) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -410,6 +415,8 @@ GEM rspec-mocks (~> 3.10) rspec-support (~> 3.10) rspec-support (3.10.3) + ruby-handlebars (0.4.0) + parslet (~> 1.6, >= 1.6.2) ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) sass (3.4.25) @@ -526,9 +533,11 @@ DEPENDENCIES puma (~> 4.3.10) rack-cors rails (~> 6.1.0) + rails-controller-testing rqrcode rspec-its rspec-rails + ruby-handlebars sass-rails simplecov simply_stored! diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index e4808dd2..a701610d 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -45,6 +45,8 @@ class DashboardController < ApplicationController # GET /select_qr_image.svg def table_qr_image @table = Table.find(params[:table_id]) + hbs = Handlebars::Handlebars.new + @qr_template = hbs.compile(@table.supplier.table_qr_template) #code = [get_the_app_url, {table_id: @table.id}.to_json].join('?') code = "q.mozo.bar/s?t=#{@table.id}" size = RQRCode.minimum_qr_size_from_string(code) @@ -53,7 +55,7 @@ class DashboardController < ApplicationController render text: "Pending table_qr_image" } format.svg { - render qrcode: code, level: :l, unit: 10, table_number: @table.number, qcontainer: true + render qrcode: code, level: :l, unit: 10, table_number: @qr_template.call(@table), qcontainer: true } format.png do render qrcode: code, level: :l, unit: 10, table_number: @table.number, qcontainer: true diff --git a/app/controllers/suppliers/tables_controller.rb b/app/controllers/suppliers/tables_controller.rb index 6f995c5b..dfb9f3f5 100644 --- a/app/controllers/suppliers/tables_controller.rb +++ b/app/controllers/suppliers/tables_controller.rb @@ -75,7 +75,7 @@ module Suppliers private def table_params - permitted_attributes = [:number, :section_id, :position_x, :position_y, :width, :height] + permitted_attributes = [:number, :label, :section_id, :position_x, :position_y, :width, :height] # do not raise in development and test for json communication if request.format.json? params.require(:table).slice(*permitted_attributes).permit! diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 227fdc84..aaf065d9 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -11,8 +11,6 @@ class Supplier product_categories.products product_categories.products.product_variants ] - - #devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable, :confirmable property :name @@ -30,6 +28,7 @@ class Supplier property :week_starts_on_monday, type: :boolean, default: true property :employee_settings_storage property :user_message + property :table_qr_template, default: '{{#if label}}{{number}} - {{label}}{{else}}{{number}}{{/if}}' # PAYMENT property :accept_bitpay, type: :boolean, default: false diff --git a/app/models/table.rb b/app/models/table.rb index 64a7e7c5..5cf6f0d0 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -4,6 +4,7 @@ class Table per_page_method :limit_value #kaminari property :number, type: Integer, default: 1 + property :label property :position_x, type: Float, default: 0 property :position_y, type: Float, default: 0 property :needs_help, type: :boolean, default: false diff --git a/config/couchdb.yml b/config/couchdb.yml index c7296f1c..7e692a84 100644 --- a/config/couchdb.yml +++ b/config/couchdb.yml @@ -7,7 +7,7 @@ test: validation_framework: :active_model #optional #database: "http://mozo:secret@localhost:5984/qwaiter_test" #Testing can only be done as couchdb admin, since it requires creating and destroying the database - database: "mozo_test" + database: "http://admin:<%= ENV['COUCHDB_ADMIN_PASSWORD']%>@localhost:5984/mozo_test" # database: "http://admin:secret@localhost:5984/qwaiter_test" production: validation_framework: :active_model #optional diff --git a/drb_counter/drb_counter.rb b/drb_counter/drb_counter.rb index 56171e92..60943b2c 100755 --- a/drb_counter/drb_counter.rb +++ b/drb_counter/drb_counter.rb @@ -72,6 +72,7 @@ class InMemoryQCounter #couch_settings = YAML.load_file(couch_settings_path)[environment] couch_settings = YAML.safe_load(ERB.new(File.read(couch_settings_path)).result, [Symbol], [], ['default'])[environment] database = couch_settings['database'].sub 'localhost', 'host.docker.internal' + #database = couch_settings['database'] #database = couch_settings['database'].sub 'localhost', '172.17.0.1' db = CouchRest.database(database) # for debug: db = CouchPotato.database.couchrest_database design_doc = "_design/order_counter" diff --git a/drb_counter/rebuild-docker.sh b/drb_counter/rebuild-docker.sh index 7585dc65..ad607169 100755 --- a/drb_counter/rebuild-docker.sh +++ b/drb_counter/rebuild-docker.sh @@ -4,6 +4,7 @@ # 1. ensure this script is run from the project's root, not the drb_counter directory pwd_dirname=$(basename $(pwd)); script_dirname="drb_counter"; +arch=$(uname) if [ "$pwd_dirname" == "$script_dirname" ]; then echo "PWD DIRNAME: "$pwd_dirname; echo "You must run this script from the project's root dir (../) for the Dockerfile to have access to the configs to COPY"; @@ -23,7 +24,12 @@ docker build -f drb_counter/Dockerfile -t mozo_drb_counter . # docker run --network=host --env DRB_ENV=production --env COUCHDB_ADMIN_PASSWORD=$COUCHDB_ADMIN_PASSWORD -t -i --rm mozo_drb_counter bash # 5. Spin up the counter container from the generated image -docker run --network=host --env DRB_ENV=production --env COUCHDB_ADMIN_PASSWORD=$COUCHDB_ADMIN_PASSWORD --add-host=host.docker.internal:host-gateway --restart unless-stopped --detach --name=mozo_drb_counter mozo_drb_counter +if [ $arch == "Darwin" ]; then + echo "Running the created image using the Mac Darwin port exposing" + docker run -p 9022:9022 --env DRB_ENV=production --env COUCHDB_ADMIN_PASSWORD=$COUCHDB_ADMIN_PASSWORD --add-host=host.docker.internal:host-gateway --restart unless-stopped --detach --name=mozo_drb_counter mozo_drb_counter +else + docker run --network=host --env DRB_ENV=production --env COUCHDB_ADMIN_PASSWORD=$COUCHDB_ADMIN_PASSWORD --add-host=host.docker.internal:host-gateway --restart unless-stopped --detach --name=mozo_drb_counter mozo_drb_counter +fi # To just start the container created through al these steps without rebuilding them: # docker container start $(docker ps -a -q --filter ancestor=mozo_drb_counter) diff --git a/lib/rqrcode-rails3/qr_container.svg b/lib/rqrcode-rails3/qr_container.svg index 33c70163..a022e911 100644 --- a/lib/rqrcode-rails3/qr_container.svg +++ b/lib/rqrcode-rails3/qr_container.svg @@ -1,6 +1,4 @@ - - @@ -178,17 +176,18 @@ inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="2.1440801" - inkscape:cx="80.481439" - inkscape:cy="258.02219" + inkscape:cx="128.05429" + inkscape:cy="314.29086" inkscape:document-units="mm" inkscape:current-layer="layer1" showgrid="false" units="mm" inkscape:window-width="1440" - inkscape:window-height="856" + inkscape:window-height="771" inkscape:window-x="0" - inkscape:window-y="0" - inkscape:window-maximized="0" /> + inkscape:window-y="25" + inkscape:window-maximized="0" + inkscape:document-rotation="0" /> @@ -211,15 +210,16 @@ transform="matrix(0.65,0,0,0.65,8.5,-112)">#qrcode #table_number + text-anchor="middle" + id="tspan3801" + style="font-family:sans-serif">#table_number @@ -227,22 +227,18 @@ x="81.717873" y="-104.40486" id="text7" - style="font-size:37.90859985px"> - MOZO - + style="font-size:29.7725px;font-family:'Arial Rounded MT Bold';-inkscape-font-specification:'Arial Rounded MT Bold';fill:#231f20">MOZO - .BAR - + style="font-family:'Arial Rounded MT Bold';-inkscape-font-specification:'Arial Rounded MT Bold'">.BAR 7] + end + + it "does render the svg image with table label if present" do + table = create :table, supplier: @supplier, number: 7, label: "The love seat" + get :table_qr_image, params: {table_id: table.id}, format: :svg + expect(response.body).to include %[id="tspan3801">7 - The love seat] + end + end +end diff --git a/spec/controllers/suppliers/tables_controller_spec.rb b/spec/controllers/suppliers/tables_controller_spec.rb index cf0cc5ef..c99fcdda 100644 --- a/spec/controllers/suppliers/tables_controller_spec.rb +++ b/spec/controllers/suppliers/tables_controller_spec.rb @@ -24,13 +24,13 @@ describe Suppliers::TablesController, type: :controller do describe "GET #show" do it "assigns the requested table to @table" do table = create :table, supplier: @supplier - get :show, id: table, format: :json + get :show, params: {id: table}, format: :json assigns(:table).should eq(table) end it "should not display a table of another supplier" do table = create :table - get :show, id: table, format: :json + get :show, params: {id: table}, format: :json response.status.should == 404 end end @@ -39,13 +39,13 @@ describe Suppliers::TablesController, type: :controller do context "with valid attributes" do it "creates a new table" do expect{ - post :create, table: {number: 22} + post :create, params: {table: {number: 22}} }.to change(Table, :count).by(1) end it "should not be possible to create a table for another supplier, linked to signed in supplier instead" do supplier2 = create :supplier - expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to change{ Table.count }.by(1) + expect { post :create, params: {table: {number: 6, supplier_id: supplier2.id}} }.to change{ Table.count }.by(1) created_table = Table.find_by_number(6) expect( created_table.supplier_id ).to eq @supplier.id end @@ -59,27 +59,27 @@ describe Suppliers::TablesController, type: :controller do context "valid attributes" do it "located the requested table" do - put :update, id: @table.id, table: {number: 22} + put :update, params: {id: @table.id, table: {number: 22}} @table.reload assigns(:table).should eq(@table) end it "changes @table's number attribute" do - put :update, id: @table, table: {number: "14"} + put :update, params: {id: @table, table: {number: "14"}} @table.reload @table.number.should eq(14) end it "should not be possible to update a table to another supplier" do supplier2 = create :supplier - put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id} + put :update, params: {id: @table.id, table: {number: 6, supplier_id: supplier2.id}} @table.reload expect( @table.supplier_id ).to eq @supplier.id end it "should not be possible to update a table of another supplier" do table = create :table, number: 11 - put :update, id: table.id, table: {number: 6} + put :update, params: {id: table.id, table: {number: 6}} table.reload table.number.should == 11 end @@ -87,7 +87,7 @@ describe Suppliers::TablesController, type: :controller do context "invalid attributes" do it "returns an error response" do - #put :update, id: @table, format: :json, table: {number: 'aaa'} + #put :update, params: {id: @table, format: :json, table: {number: 'aaa'}} #TODO: when proper invalid tables exist expect( JSON.parse(response.body)['errors'] ).to be_present end end @@ -100,14 +100,14 @@ describe Suppliers::TablesController, type: :controller do it "deletes the table" do expect{ - delete :destroy, id: @table + delete :destroy, params: {id: @table} }.to change(Table, :count).by(-1) end it "should not be possible to delete a table of another supplier" do table = create :table expect{ - delete :destroy, id: table + delete :destroy, params: {id: table} }.to_not change(Table, :count) end end