Class: Images::Tags::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/images/tags/helpers.rb

Defined Under Namespace

Classes: TagError

Constant Summary collapse

CONDITIONS =
['images.position','images.title','images.id']

Class Method Summary collapse

Class Method Details

.current_image(tag) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/images/tags/helpers.rb', line 32

def current_image(tag)
  @conditions = CONDITIONS.dup
  
  # Images exist, and we're not looking to change the scope
  if tag.locals.image.present? and image_conditions(tag).empty?
    return tag.locals.image
  end
  
  unless tag.locals.images.nil?
    images = tag.locals.images
    if images.first.is_a?(Image)
      query = Image.all image_conditions(tag).merge(image_options(tag))
    else
      @conditions.map! { |term| term.gsub('images.position','attachments.position') }
      query = Attachment.all image_conditions(tag).merge(image_options(tag)).merge(:joins => 'JOIN images ON images.id = attachments.image_id')
      query = query.map { |a| a.image }
    end
    return (query && images).first
  else
    return Image.first image_conditions(tag).merge(image_options(tag))
  end
end

.current_images(tag) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/images/tags/helpers.rb', line 11

def current_images(tag)
  @conditions = CONDITIONS.dup
  
  if tag.locals.images.nil?
    return Image.all image_conditions(tag).merge(image_options(tag))
  end

  if tag.locals.images.empty?
    return tag.locals.images
  end

  images = tag.locals.images
  if images.first.is_a?(Image)
    images.all image_conditions(tag).merge(image_options(tag))
  else            
    # We're looking based on attachment positions, not image positions
    @conditions.map! { |term| term.gsub('images.position','attachments.position') }
    images.all image_conditions(tag).merge(image_options(tag)).merge(:joins => 'JOIN images ON images.id = attachments.image_id')
  end
end