Skip to content

Commit

Permalink
Merge pull request rubyforgood#1131 from rebecarancan/rebecarancan/ru…
Browse files Browse the repository at this point in the history
…byforgood#1088-upload-casa-org-logo

Admin can upload casa_org logo rubyforgood#1088
  • Loading branch information
compwron authored Oct 21, 2020
2 parents 8e6b652 + 80eb973 commit dc2821c
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 7 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ARG RAILS_ROOT=/usr/src/app/

RUN apk update && apk upgrade && apk add --update --no-cache \
bash \
imagemagick \
nodejs \
postgresql-client \
tzdata \
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ gem "puma", "~> 5.0" # Use Puma as the app server
gem "pundit" # for authorization management - based on user.role field
gem "skylight" # automated performance testing https://www.skylight.io/
gem "webpacker", "~> 5.2" # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem "image_processing", "~> 1.2" # Set of higher-level helper methods for image processing.

gem "bootsnap", ">= 1.4.2", require: false # Reduces boot times through caching; required in config/boot.rb
gem "bugsnag" # tracking errors in prod
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ GEM
html_tokenizer (0.0.7)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
image_processing (1.12.0)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
jbuilder (2.10.1)
activesupport (>= 5.0.0)
launchy (2.5.0)
Expand All @@ -162,6 +165,7 @@ GEM
mimemagic (~> 0.3.2)
method_source (1.0.0)
mimemagic (0.3.5)
mini_magick (4.10.1)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.14.2)
Expand Down Expand Up @@ -263,6 +267,8 @@ GEM
rubocop (>= 0.87.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.10.1)
ruby-vips (2.0.17)
ffi (~> 1.9)
rubyzip (2.3.0)
sablon (0.3.2)
nokogiri (>= 1.8.5)
Expand Down Expand Up @@ -346,6 +352,7 @@ DEPENDENCIES
erb_lint
factory_bot_rails
faker
image_processing (~> 1.2)
jbuilder (~> 2.10)
letter_opener
listen (>= 3.0.5, < 3.3)
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/casa_orgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def edit
def update
respond_to do |format|
if @casa_org.update(casa_org_update_params)
@casa_org.logo.attach(casa_org_update_params[:logo])
format.html { redirect_to edit_casa_org_path, notice: "CASA organization was successfully updated." }
format.json { render :show, status: :ok, location: @casa_org }
else
Expand All @@ -30,7 +31,7 @@ def set_casa_org
end

def casa_org_update_params
params.require(:casa_org).permit(:name, :display_name, :address)
params.require(:casa_org).permit(:name, :display_name, :address, :logo)
end

def set_contact_type_data
Expand Down
1 change: 1 addition & 0 deletions app/models/casa_org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CasaOrg < ApplicationRecord
has_many :contact_type_groups, dependent: :destroy
has_one :casa_org_logo, dependent: :destroy
has_many :hearing_types, dependent: :destroy
has_one_attached :logo

delegate :url, :alt_text, :size, to: :casa_org_logo, prefix: :logo, allow_nil: true

Expand Down
5 changes: 5 additions & 0 deletions app/views/casa_orgs/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<%= form.label :address %>
<%= form.text_field :address, class: "form-control" %>
</div>

<div class="custom-file mb-5">
<%= form.label :logo %>
<%= form.file_field :logo, class: "form-control" %>
</div>
<br>

<div class="actions">
Expand Down
9 changes: 4 additions & 5 deletions app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<div id="sidebar-js" class="sidebar-wrapper">
<div class="sidebar-heading">
<% if all_casa_admin_signed_in? %>
<div class="sidebar-heading text-center">
<% if all_casa_admin_signed_in? || !current_organization.logo.attached? %>
<%= image_pack_tag("media/src/images/default-logo.png",
alt: "CASA Logo",
class: "d-inline-block align-text-bottom") %>
<% else %>
<%= image_pack_tag(current_organization.logo_url,
alt: current_organization.logo_alt_text || "CASA Logo",
class: "d-inline-block align-text-bottom") if current_organization&.logo_url %>
<%= image_tag(current_organization.logo.variant(resize_to_limit: [200, 100]),
alt: current_organization.logo_alt_text || "CASA Logo") %>
<% end %>
</div>
<div class="sidebar-container">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
24 changes: 23 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_10_13_171632) do
ActiveRecord::Schema.define(version: 2020_10_19_120548) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "all_casa_admins", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down Expand Up @@ -183,6 +204,7 @@
t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "casa_cases", "casa_orgs"
add_foreign_key "casa_org_logos", "casa_orgs"
add_foreign_key "case_assignments", "casa_cases"
Expand Down
Binary file added spec/fixtures/company_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions spec/models/casa_org_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
expect(org.logo_url).to eq "foo.com"
end
end

describe 'Attachment' do
it 'is valid' do
subject.logo.attach(io: File.open("#{Rails.root}/spec/fixtures/company_logo.png"),
filename: 'company_logo.png', content_type: 'logo/png')
expect(subject.logo).to be_attached
end
end
end
7 changes: 7 additions & 0 deletions spec/requests/casa_orgs_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
RSpec.describe "CasaOrgs", type: :request do
let(:casa_org) { create(:casa_org) }
let(:valid_attributes) { {name: "name", display_name: "display_name", address: "address"} }
let(:logo) { fixture_file_upload("#{Rails.root}/spec/fixtures/company_logo.png", 'image/png') }
let(:invalid_attributes) { {name: nil} }
let(:casa_case) { create(:casa_case, casa_org: casa_org) }

Expand All @@ -26,6 +27,12 @@
expect(casa_org.address).to eq "address"
end

it "uploads the company logo" do
expect {
patch casa_org_url(casa_org), params: { casa_org: { logo: logo } }
}.to change(ActiveStorage::Attachment, :count).by(1)
end

it "redirects to the casa_org" do
patch casa_org_url(casa_org), params: {casa_org: valid_attributes}
casa_org.reload
Expand Down

0 comments on commit dc2821c

Please sign in to comment.