Skip to content

Commit

Permalink
Merge pull request #88 from Floppy/images
Browse files Browse the repository at this point in the history
Add Images
  • Loading branch information
Floppy authored Mar 21, 2021
2 parents aa59afe + 2c37a77 commit 67b3e9f
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 1 deletion.
27 changes: 27 additions & 0 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ImagesController < ApplicationController
before_action :get_library
before_action :get_model
before_action :get_image

def show
respond_to do |format|
format.png { send_file File.join(@library.path, @model.path, @image.filename) }
format.jpeg { send_file File.join(@library.path, @model.path, @image.filename) }
end
end

private

def get_library
@library = Library.find(params[:library_id])
end

def get_model
@model = @library.models.find(params[:model_id])
end

def get_image
@image = @model.images.find(params[:id])
@title = @image.name
end
end
7 changes: 7 additions & 0 deletions app/jobs/model_scan_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ModelScanJob < ApplicationJob
queue_as :default

FILE_PATTERN = "*.{stl,STL,obj,OBJ}"
IMAGE_FILE_PATTERN = "*.{jpg,JPG,png,PNG}"

def perform(model)
# For each file in the model, create a part
Expand All @@ -13,6 +14,12 @@ def perform(model)
]).each do |filename|
model.parts.find_or_create_by(filename: filename.gsub(model_path + "/", ""))
end
Dir.glob([
File.join(dir.path, IMAGE_FILE_PATTERN),
File.join(dir.path, "images", IMAGE_FILE_PATTERN)
]).each do |filename|
model.images.find_or_create_by(filename: filename.gsub(model_path + "/", ""))
end
end
# Set tags and default parts
model.parts.reload
Expand Down
14 changes: 14 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Image < ApplicationRecord
belongs_to :model
validates :filename, presence: true, uniqueness: {scope: :model}

default_scope { order(:filename) }

def file_format
File.extname(filename).delete(".").downcase
end

def name
File.basename(filename, ".*").humanize.titleize
end
end
1 change: 1 addition & 0 deletions app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Model < ApplicationRecord
belongs_to :library
belongs_to :creator, optional: true
has_many :parts, dependent: :destroy
has_many :images, dependent: :destroy
belongs_to :preview_part, class_name: "Part", optional: true
validates :name, presence: true
validates :path, presence: true, uniqueness: {scope: :library}
Expand Down
8 changes: 8 additions & 0 deletions app/views/models/_image.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="col mb-4">
<div class="card">
<%= image_tag library_model_image_path(@library, @model, image, format: image.file_format), class: "card-img-top", alt: image.name %>
<div class="card-body">
<h5 class="card-title"><%= image.name %></h5>
</div>
</div>
</div>
5 changes: 5 additions & 0 deletions app/views/models/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class="row row-cols-2">

<div class="col-9">
<h2>Parts</h2>
<div class="row row-cols-2 row-cols-md-3 mb-4">
<%= render partial: "part", collection: @groups.delete(nil) %>
</div>
Expand All @@ -11,6 +12,10 @@
<%= render partial: "part", collection: parts %>
</div>
<% end %>
<h2>Images</h2>
<div class="row row-cols-2 row-cols-md-3 mb-4">
<%= render partial: "image", collection: @model.images %>
</div>
</div>

<div class="col-3">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
patch "update", action: "bulk_update"
end
resources :parts, except: [:index, :destroy]
resources :images, only: [:show]
end
end
resources :creators
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20210321164508_create_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateImages < ActiveRecord::Migration[6.1]
def change
create_table :images do |t|
t.references :model, null: false, foreign_key: true
t.string :filename
t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/models/image_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe Image, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 67b3e9f

Please sign in to comment.