Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload the calculator logo #982

Open
wants to merge 17 commits into
base: calculators-constructor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/account/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index
end

def show
# TODO: fill it
@calculator = resource
end

def new
Expand Down Expand Up @@ -81,7 +81,7 @@ def collect_fields_for_kind(kind)

def calculator_params
params.require(:calculator).permit(
:id, :en_name, :uk_name,
:id, :en_name, :uk_name, :logo_picture,
formulas_attributes: [:id, :expression, :en_label, :uk_label, :calculator_id, :en_unit, :uk_unit, :_destroy],
fields_attributes: [:id, :en_label, :uk_label, :var_name, :kind, :_destroy,
categories_attributes: [:id, :en_name, :uk_name, :price, :_destroy]]
Expand Down
2 changes: 2 additions & 0 deletions app/models/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class Calculator < ApplicationRecord
friendly_id :en_name, use: :sequentially_slugged

translates :name
attribute :logo_placeholder, :string, default: "https://via.placeholder.com/428x307?text=Logo"

has_one_attached :logo_picture
has_many :fields, dependent: :destroy
has_many :formulas, dependent: :destroy

Expand Down
2 changes: 2 additions & 0 deletions app/views/account/calculators/partials/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<legend class="admin-legend">Calculator</legend>
<%= f.input :en_name, label: "Calculator Name:", class: 'form-control' %>
<%= f.input :uk_name, label: "Uk Calculator Name:", class: 'form-control' %>
<%= f.label :logo_picture, "Upload logo:", class: "form-label d-block" %>
<%= f.file_field :logo_picture, accept: "image/jpeg, image/png", class: "file-input mb-3" %>
</fieldset>

<!-- formula input-->
Expand Down
2 changes: 2 additions & 0 deletions app/views/account/calculators/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<p class="font-bold"> <%= @calculator.slug %> </p>
</div>

<%= render "calculators/partials/logo_picture", calculator: @calculator %>

<div class="showpage-buttons">
<%= link_to t('.edit'),
edit_account_calculator_path(@calculator.slug, locale: I18n.locale),
Expand Down
5 changes: 5 additions & 0 deletions app/views/calculators/partials/_logo_picture.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if @calculator.logo_picture.attached? %>
<%= image_tag(@calculator.logo_picture.variant(resize_to_fill: [428, 307])) %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use constant for this dimension definition

<% else %>
<%= image_tag(@calculator.logo_placeholder) %>
<% end %>
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
t.bigint "calculator_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "priority", default: 0, null: false
t.index ["calculator_id"], name: "index_formulas_on_calculator_id"
end

Expand Down
10 changes: 8 additions & 2 deletions spec/models/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

RSpec.describe Calculator, type: :model do
let(:local_prefix_calculator) { "activerecord.errors.models.calculator.attributes" }

subject { build(:calculator) }
let(:calculator) { build(:calculator) }

describe "validations" do
it { is_expected.to validate_presence_of(:en_name) }
Expand All @@ -31,7 +30,14 @@
end

describe "associations" do
it { is_expected.to have_one_attached(:logo_picture) }
it { is_expected.to have_many(:fields).dependent(:destroy) }
it { is_expected.to have_many(:formulas).dependent(:destroy) }
end

describe "logo_placeholder attribute" do
it "has a default value" do
expect(calculator.logo_placeholder).to eq("https://via.placeholder.com/428x307?text=Logo")
end
end
end
11 changes: 11 additions & 0 deletions spec/requests/account/calculators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@
end
end

describe "GET #show" do
include_context :in_local_environment

it "renders the calculator template" do
get account_calculator_path(slug: calculator.slug)

expect(response).to be_successful
expect(response).to render_template(:show)
end
end

describe "GET #new" do
subject { get new_path }

Expand Down
Loading