Class: Interview::View

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

Instance Attribute Summary collapse

Attributes inherited from Control

#parent

Instance Method Summary collapse

Methods inherited from Control

#ancestors, #build_child, #build_with_params, #find_attribute, #find_attribute!, #initialize, #set_attributes, #set_defaults

Constructor Details

This class inherits a constructor from Interview::Control

Instance Attribute Details

#assoc_methodObject (readonly)

Returns the value of attribute assoc_method.



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

def assoc_method
  @assoc_method
end

#assoc_objectObject (readonly)

Returns the value of attribute assoc_object.



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

def assoc_object
  @assoc_object
end

#auto_title_forObject

Returns the value of attribute auto_title_for.



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

def auto_title_for
  @auto_title_for
end

#filterObject

Returns the value of attribute filter.



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

def filter
  @filter
end

#objectObject

Returns the value of attribute object.



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

def object
  @object
end

#objectsObject

Returns the value of attribute objects.



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

def objects
  @objects
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#build(b) ⇒ Object



54
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
# File 'lib/interview/controls/view.rb', line 54

def build(b)
  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 = @parent.find_attribute! :object
      @object = @object.split('.').inject(@assoc_object, :send)
    # end
  elsif @object.is_a? Proc  
    @assoc_object = @parent.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
  
  yield if block_given?
  
  @object = tmp_object
  @objects = tmp_objects
end

#nested_formObject



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

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

#singular_titleObject

todo: Überdenken



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

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



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

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