Class: Utopia::Gallery::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/gallery/tags.rb

Constant Summary collapse

DEFAULT_PROCESSES =
[
	ResizeImage.new(:small, [400, 400], :resize_to_fill),
	ResizeImage.new(:medium, [800, 800], :resize_to_fill),
	ResizeImage.new(:large, [1600, 1600], :resize_to_fit),
]

Instance Method Summary collapse

Constructor Details

#initialize(media_root: Utopia.default_root, cache_root: Utopia.default_root('public/_gallery'), cache_path: '/_gallery', processes: DEFAULT_PROCESSES, container_class: 'gallery') ⇒ Tags

Returns a new instance of Tags.

Parameters:

  • media_root (String) (defaults to: Utopia.default_root)

    Directory where media is stored.

  • cache_root (String) (defaults to: Utopia.default_root('public/_gallery'))

    Directory where media is cached.

  • cache_root (String) (defaults to: Utopia.default_root('public/_gallery'))

    The prefix path for the cached assets, served as static content.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/utopia/gallery/tags.rb', line 39

def initialize(media_root: Utopia.default_root, cache_root: Utopia.default_root('public/_gallery'), cache_path: '/_gallery', processes: DEFAULT_PROCESSES, container_class: 'gallery')
	@media_root = media_root
	@cache_root = cache_root
	@cache_path = cache_path
	@processes = {}
	
	@container_class = container_class
	
	processes.each do |process|
		name = process.name
		
		raise ArgumentError.new("Duplicate process #{name}") if @processes.include?(name)
		
		@processes[name] = process
	end
end

Instance Method Details

#call(name, node) ⇒ Object



81
82
83
84
# File 'lib/utopia/gallery/tags.rb', line 81

def call(name, node)
	# TODO: Validate security implications/leaky abstraction.
	self.method(name)
end

#container(document, state) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/utopia/gallery/tags.rb', line 56

def container(document, state)
	node = document.parent.node
	path = node.uri_path.dirname + Utopia::Path[state[:path]]
	
	options = {}
	if filetypes = state[:filetypes]
		options[:filter] = Regexp.new("(#{filetypes})$", Regexp::IGNORECASE)
	elsif filter = state[:filter]
		options[:filter] = Regexp.new(filter, Regexp::IGNORECASE)
	end
	
	# Where should we get this from?
	container = Container.new(@media_root, path, **options)
	
	media_tag_name = state[:tag] || 'img'
	container_class = state[:class] || @container_class
	
	document.tag('div', class: container_class) do
		container.sort.each do |media|
			cache = Cache.new(@media_root, @cache_root, @cache_path, media, @processes).update
			document.tag(media_tag_name, src: cache, alt: media)
		end
	end
end