Module: PathBuilder

Extended by:
ActiveSupport::Concern
Included in:
Model
Defined in:
app/models/concerns/path_builder.rb

Instance Method Summary collapse

Instance Method Details

#formatted_path ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/concerns/path_builder.rb', line 4

def formatted_path
  library.path_template.gsub(/{.+?}/) do |token|
    case token
    when "{tags}"
      tags.exists? ?
        File.join(tags.order(taggings_count: :desc, name: :asc).map { it.to_s.parameterize }) :
        "@untagged"
    when "{creator}"
      path_component(creator) || "@unattributed"
    when "{collection}"
      path_component(collections.order(created_at: :asc).first) || "@uncollected"
    when "{collections}"
      scope = collections.order(models_count: :desc, name: :asc)
      collections = scope.where(collection_id: nil).to_a
      while scope.count > collections.count
        collections.concat scope.where(collection_id: collections.map(&:id)).to_a
      end
      File.join(collections.map { path_component(it) })
    when "{modelName}"
      path_component(self)
    when "{modelId}"
      "##{id}"
    else
      token
    end
  end
end