Class: RailsDevtools::ImageSearchForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/forms/rails_devtools/image_search_form.rb

Instance Method Summary collapse

Constructor Details

#initialize(search: "") ⇒ ImageSearchForm

Returns a new instance of ImageSearchForm.



7
8
9
# File 'app/forms/rails_devtools/image_search_form.rb', line 7

def initialize(search: "")
  @search = search
end

Instance Method Details

#resultsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/forms/rails_devtools/image_search_form.rb', line 11

def results
  folders = RailsDevtools.asset_config.paths

  images_by_folder = Hash.new { |hash, key| hash[key] = [] }
  extensions = ImageAssets::ImageInfo::IMAGE_EXTENSIONS.map { |ext| ext.delete_prefix(".") }.join(",")

  folders.each do |dir|
    Dir.glob("#{dir}/**/*{#{@search.upcase}, #{@search.downcase}}*.{#{extensions}}").each do |path|
      image_info = ImageAssets::ImageInfo.new(path)
      next unless image_info.valid?

      dir = File.dirname(path).gsub(Rails.root.to_s, "")
      images_by_folder[dir] << image_info
    end
  end

  images_by_folder
end