Class: Cms::DataTag

Inherits:
Liquid::Tag show all
Defined in:
app/liquid/tags/cms/data_tag.rb

Direct Known Subclasses

AssetDataTag

Defined Under Namespace

Modules: TagMethods

Instance Attribute Summary collapse

Attributes inherited from Liquid::Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Liquid::Tag

#name, #parse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ DataTag

Returns a new instance of DataTag.



9
10
11
12
# File 'app/liquid/tags/cms/data_tag.rb', line 9

def initialize(tag_name, markup, tokens)
  @markup = markup
  super
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'app/liquid/tags/cms/data_tag.rb', line 6

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'app/liquid/tags/cms/data_tag.rb', line 7

def options
  @options
end

Instance Method Details

#context_objectObject



14
15
16
# File 'app/liquid/tags/cms/data_tag.rb', line 14

def context_object
  TagMethods.context_object(@context)
end

#paramsObject



18
19
20
# File 'app/liquid/tags/cms/data_tag.rb', line 18

def params
  TagMethods.params(@context)
end

#render(context) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/liquid/tags/cms/data_tag.rb', line 22

def render(context)
  @context = context
  @options = TagMethods.parse_options(context, @markup)

  get_data do |name, data|
    context[@options[:as] || name] = data
  end
  ''
end

#uses_random(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/liquid/tags/cms/data_tag.rb', line 32

def uses_random(&block)
  collection = []

  # random sql func supported by postgresql and sqlite (perhaps others)
  random_func = "random()"

  begin
    collection = yield random_func
  rescue ActiveRecord::StatementInvalid => e
    if options[:random] == true
      # the random function used was invalid, so we'll try an alternative syntax for mysql (perhaps others)
      mysql_func = "rand()"

      if random_func != mysql_func
        random_func = mysql_func
      else
        # set random to false and just use the default order since the alt didn't work either
        options[:random] = false
      end

      # retry the query
      retry
    end
  end

  collection
end