Class: Interview::View

Inherits:
Control show all
Includes:
HasControls
Defined in:
lib/interview/view.rb

Instance Attribute Summary collapse

Attributes included from HasControls

#controls

Attributes inherited from Control

#parent

Instance Method Summary collapse

Methods included from HasControls

#add_control, #add_controls, included, #initialize, #siblings

Methods inherited from Control

#ancestors, build, definition, #find_attribute, #find_attribute!, inherited, #initialize, #set_attributes, #set_defaults

Instance Attribute Details

#assoc_methodObject (readonly)

Returns the value of attribute assoc_method.



6
7
8
# File 'lib/interview/view.rb', line 6

def assoc_method
  @assoc_method
end

#assoc_objectObject (readonly)

Returns the value of attribute assoc_object.



6
7
8
# File 'lib/interview/view.rb', line 6

def assoc_object
  @assoc_object
end

#auto_title_forObject

Returns the value of attribute auto_title_for.



5
6
7
# File 'lib/interview/view.rb', line 5

def auto_title_for
  @auto_title_for
end

#filterObject

Returns the value of attribute filter.



5
6
7
# File 'lib/interview/view.rb', line 5

def filter
  @filter
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/interview/view.rb', line 5

def object
  @object
end

#objectsObject

Returns the value of attribute objects.



5
6
7
# File 'lib/interview/view.rb', line 5

def objects
  @objects
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/interview/view.rb', line 5

def scope
  @scope
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/interview/view.rb', line 5

def title
  @title
end

Instance Method Details

#nested_formObject



8
9
10
# File 'lib/interview/view.rb', line 8

def nested_form
  return siblings.find { |sib| sib.is_a? Interview::NestedForm }
end

#renderObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/interview/view.rb', line 55

def render
  tmp_object = @object
  tmp_objects = @objects
  
  if @object.is_a? String
    # if Object::const_defined?(@object.camelcase) # todo: Überdenken!
    #   @object = @object.camelcase.constantize.new
    # else
      @assoc_object = find_attribute! :object
      @object = @object.split('.').inject(@assoc_object, :send)
    # end
  elsif @object.is_a? Proc  
    @assoc_object = find_attribute! :object
    @object = @object.call(@assoc_object, self)
  elsif @object.is_a? Class
    @object = @object.new
  elsif @object.nil?
    @object = find_attribute! :object
  end
  
  if @objects.is_a? String
    @assoc_object = find_attribute! :object
    @assoc_method = @objects
    
    @objects = @assoc_method.split('.').inject(@assoc_object, :send)
    # @objects = @assoc_object.send @assoc_method
    @objects = @objects.send @scope if @scope
    @object = @objects.klass.new
    @scope = nil
    @filter = { "#{assoc_object.class.name.underscore}_id" => assoc_object.id }
  end
  
  if Object.const_defined? 'Gretel' and parent.nil?
    model_name = @object.class.lookup_ancestors.last.model_name
    case h.action_name
    when 'index'
      h.breadcrumb model_name.plural.to_sym
    when 'show'
      h.breadcrumb model_name.singular.to_sym, @object
    when 'new'
      h.breadcrumb "new_#{model_name.singular}".to_sym, @object
    when 'edit'
      h.breadcrumb "edit_#{model_name.singular}".to_sym, @object
    end
  end
  
  html = @controls.map { |c| c.render }.join
  
  @object = tmp_object
  @objects = tmp_objects
  return html
end

#singular_titleObject

todo: Überdenken



32
33
34
35
36
37
38
39
40
# File 'lib/interview/view.rb', line 32

def singular_title # todo: Überdenken
  if @auto_title_for
    if @assoc_object
      return @assoc_object.class.human_attribute_name(@assoc_method.to_s.singularize)
    else
      return @object.class.human_name
    end
  end
end

#tooltipObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/interview/view.rb', line 42

def tooltip
  defaults = @object.class.lookup_ancestors.map do |klass|
    klass.name.underscore.to_sym
  end
  defaults << ''
  
  options = { :scope => [@object.class.i18n_scope, :model_tooltips], :default => defaults }
  return I18n.translate(defaults.shift, options)
  
  # return h.t("activerecord.model_tooltips.#{@object.class.name.underscore}", default: '')
  # todo: tooltip von Elternelement verwenden, falls nicht vorhanden.
end