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
# 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) do
    if 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 }
        scope.even_odd = scope.even_odd == "even" ? "odd" : "even"
      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
        scope.even_odd = scope.even_odd == "even" ? "odd" : "even"
        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)


51
52
53
54
55
56
57
# File 'lib/dryml/helper.rb', line 51

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)


60
61
62
63
64
65
66
# File 'lib/dryml/helper.rb', line 60

def last_item?
  if 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



68
69
70
71
72
# File 'lib/dryml/helper.rb', line 68

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/dryml/helper.rb', line 75

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



86
87
88
# File 'lib/dryml/helper.rb', line 86

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