Module: Dryml::Helper

Included in:
TemplateEnvironment
Defined in:
lib/dryml/helper.rb

Overview

An ActionView Helper

Instance Method Summary collapse

Instance Method Details

#context_map(enum = this) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dryml/helper.rb', line 19

def context_map(enum = this)
  # TODO: Calls to respond_to? in here can cause the full collection hiding behind a scoped collection to get loaded
  res = []
  empty = true
  scope.new_scope(:repeat_collection => enum, :even_odd => 'odd', :repeat_item => nil, :index => 0) do
    if !enum.respond_to?(:to_a) && enum.respond_to?(:each_pair)
      enum.each_pair do |key, value|
        scope.repeat_item = value
        empty = false;
        self.this_key = key;
        new_object_context(value) { res << yield }
        # The lines below are only used to prepare for the next element of the collection
        scope.even_odd = scope.even_odd == "even" ? "odd" : "even"
        scope.index += 1
      end
    else
      index = 0
      enum.each do |e|
        scope.repeat_item = e
        empty = false;
        if enum == this
          new_field_context(index, e) { res << yield }
        else
          new_object_context(e) { res << yield }
        end
        # The lines below are only used to prepare for the next element of the collection
        scope.even_odd = scope.even_odd == "even" ? "odd" : "even"
        index += 1
        scope.index += 1
      end
    end
    Dryml.last_if = !empty
  end
  res
end

#create_part_id(part_name, part_locals, binding) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dryml/helper.rb', line 3

def create_part_id(part_name, part_locals, binding)
  locals={}
  part_locals.split(',').each do |local|
    local=local.strip
    locals[local] = eval(local, binding)
  end
  key2=[typed_id, locals.to_yaml]
  @part_ids ||= {}
  if @part_ids[part_name]
    @part_ids[part_name][key2] ||= "#{part_name}-#{@part_ids[part_name].length.to_s}"
  else
    @part_ids[part_name] = {key2 => part_name}
    part_name
  end
end

#first_item?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/dryml/helper.rb', line 55

def first_item?
  if scope.repeat_collection.respond_to? :each_pair
    this == scope.repeat_collection.first.try.last
  else
    this == scope.repeat_collection.first
  end
end

#last_item?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/dryml/helper.rb', line 64

def last_item?
  if !scope.repeat_collection.respond_to?(:to_a) && scope.repeat_collection.respond_to?(:each_pair)
    this == scope.repeat_collection.last.try.last
  else
    this == scope.repeat_collection.last
  end
end

#param_name_for(path) ⇒ Object



72
73
74
75
76
# File 'lib/dryml/helper.rb', line 72

def param_name_for(path)
  field_path = field_path.to_s.split(".") if field_path.is_one_of?(String, Symbol)
  attrs = path.rest.map{|part| "[#{part.to_s.sub /\?$/, ''}]"}.join
  "#{path.first}#{attrs}"
end

#param_name_for_this(foreign_key = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/dryml/helper.rb', line 79

def param_name_for_this(foreign_key=false)
  return "" unless form_this
  name = if foreign_key && (refl = this_field_reflection) && refl.macro == :belongs_to
           param_name_for(path_for_form_field[0..-2] + [refl.foreign_key])
         else
           param_name_for(path_for_form_field)
         end
  register_form_field(name)
  name
end

#param_name_for_this_parentObject



90
91
92
# File 'lib/dryml/helper.rb', line 90

def param_name_for_this_parent
  param_name_for(path_for_form_field[0..-2])
end