Class: Lolita::Configuration::Field::Array

Inherits:
Base show all
Includes:
Hooks
Defined in:
lib/lolita/configuration/field/array.rb

Overview

Fields with Array is used to

  • create select for belongs_to association or

  • has_and_belongs_to_many field for selecting all associated objects or

  • for polymorphic belongs_to associations

Polymorphic builder (:polymorphic)

Create two select boxes one of all related classes and second with related records for that class. Related classes can have polymorphic_select_name instance method, that is used to populate second select with visible values by default it calles text_method. It will fallback first content column. Class should respond to one of these.

Constant Summary collapse

MAX_RECORD_COUNT =
100

Instance Attribute Summary

Attributes inherited from Base

#dbi, #dbi_field, #nested_in

Instance Method Summary collapse

Methods included from Hooks

included, method_missing

Methods inherited from Base

#find_dbi_field, #match_state_of?, #name=, #nested?, #nested_in?, #record_state_matches_with, #set_attributes, #title, #type, #type=

Methods included from Builder

#build, #builder, #builder=, #builder_default_name, #builder_default_options, #builder_default_state

Constructor Details

#initialize(dbi, name, *args, &block) ⇒ Array

Returns a new instance of Array.



22
23
24
25
26
27
28
29
30
31
# File 'lib/lolita/configuration/field/array.rb', line 22

def initialize dbi,name,*args, &block
  @include_blank=true
  super
  self.find_dbi_field unless self.dbi_field

  @association ||= self.dbi_field ? self.dbi_field.association : detect_association
  self.run(:after_association_loaded)
  self.builder = detect_builder unless @builder
  self.name = recognize_real_name
end

Instance Method Details

#association_values(record = nil) ⇒ Object

Collect values for array type field. Uses text_method for content. By default it search for first String type field in DB. Uses value_method for value, by default it it is id.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lolita/configuration/field/array.rb', line 61

def association_values(record = nil) #TODO test
  @association_values=if values
    values
  elsif search
    search.run("")
  elsif @association && @association.polymorphic?
    polymorphic_association_values(record)
  elsif @association
    klass=@association.klass
    options_array(collect_records_for(klass))
  else
    []
  end
  @association_values
end

#create_search(*args, &block) ⇒ Object



43
44
45
# File 'lib/lolita/configuration/field/array.rb', line 43

def create_search *args, &block
  Lolita::Configuration::Search.new(Lolita::DBI::Base.create(@association.klass),*args,&block)
end

#current_text_method(klass) ⇒ Object



97
98
99
# File 'lib/lolita/configuration/field/array.rb', line 97

def current_text_method(klass)
  @text_method || default_text_method(klass)
end

#current_value_methodObject



101
102
103
# File 'lib/lolita/configuration/field/array.rb', line 101

def current_value_method
  @value_method || :id
end

#detect_associationObject



130
131
132
133
134
135
136
# File 'lib/lolita/configuration/field/array.rb', line 130

def detect_association
  unless @association
    dbi.associations[self.name.to_s]
  else
    @association
  end
end

#detect_builderObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lolita/configuration/field/array.rb', line 116

def detect_builder
  if @association
    if @association.polymorphic?
      "polymorphic"
    elsif @association.macro == :many_to_many
      "autocomplete"
    else
      "select"
    end
  else
    "select"
  end
end

#options_array(collection) ⇒ Object



90
91
92
93
94
95
# File 'lib/lolita/configuration/field/array.rb', line 90

def options_array(collection)
  klass = collection.last ? collection.last.class : nil
  collection.map{|r|
    [r.send(current_text_method(klass)),r.send(current_value_method)]
  }
end

#polymorphic_association_values(options = {}) ⇒ Object

Collect values for polymorphic association, you may pass

  • :klass - class that’s records are used

  • :record - record class that has polymorphic association. It is used to call to detect related object class.



80
81
82
83
84
85
86
87
88
# File 'lib/lolita/configuration/field/array.rb', line 80

def polymorphic_association_values(options={})
  options ||= {}
  options[:klass] ||= options[:record] && options[:record].send(self.name) ? options[:record].send(self.name).class : nil
  if options[:klass]
    options_array(collect_records_for(options[:klass]))
  else
    []
  end
end

#polymorphic_classesObject



138
139
140
141
142
143
144
145
146
# File 'lib/lolita/configuration/field/array.rb', line 138

def polymorphic_classes
  if @related_classes
    @related_classes.map do |klass|
      [klass.constantize.lolita_model_name.human, klass.to_s]
    end
  else
    []
  end
end

#recognize_real_nameObject



148
149
150
151
152
153
154
155
# File 'lib/lolita/configuration/field/array.rb', line 148

def recognize_real_name
  if @association && !@association.polymorphic? && @association.macro == :one
    @real_name = self.name
    self.name = @association.key
  else
    @name
  end
end

#search(*args, &block) ⇒ Object

For details see Lolita::Configuration::Search



34
35
36
37
38
39
40
41
# File 'lib/lolita/configuration/field/array.rb', line 34

def search *args, &block
  if (args && args.any?) || block_given?
    self.after_association_loaded do
      @search = create_search(*args,&block)
    end
  end
  @search
end

#values(value = nil, &block) ⇒ Object

Use this with block if values are dynamicly collected.



52
53
54
55
# File 'lib/lolita/configuration/field/array.rb', line 52

def values value=nil, &block
  @values=value || block if value || block_given?
  @values
end

#values=(value = nil) ⇒ Object



47
48
49
# File 'lib/lolita/configuration/field/array.rb', line 47

def values=(value=nil)
  @values=value
end

#view_values(view) ⇒ Object

used in views for shorter accessing to values



106
107
108
109
110
111
112
113
114
# File 'lib/lolita/configuration/field/array.rb', line 106

def view_values(view)
  record = view.send(:current_form).object
  values = association_values(record)
  if values.respond_to?(:call)
    values.call(view)
  else
    association_values(record)
  end
end