Class: Lolita::Configuration::Tab::Base

Inherits:
Object
  • Object
show all
Includes:
Builder
Defined in:
lib/lolita/configuration/tab.rb

Direct Known Subclasses

Content, Default

Constant Summary collapse

@@available_types =

For different types there are different builders(cells)

[:content]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Builder

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

Constructor Details

#initialize(dbi, type, *args, &block) ⇒ Base

To create new tab the following parametrs need to be provided.

  • dbi Lolita::DBI::Base object, that represents database.

  • *args See #set_attributes, for how these args are processed.

  • &block Block can be passed, anything in block will be evaled for current instance.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lolita/configuration/tab.rb', line 35

def initialize dbi,type,*args,&block
  @fields = Lolita::Configuration::Fields.new
  @field_sets = []
  @nested_forms = []
  @type = type
  self.dbi=dbi
  self.current_dbi=dbi
  self.set_attributes(*args)
  self.instance_eval(&block) if block_given?
  set_default_attributes
end

Instance Attribute Details

#current_dbiObject

Returns the value of attribute current_dbi.



28
29
30
# File 'lib/lolita/configuration/tab.rb', line 28

def current_dbi
  @current_dbi
end

#current_fieldsetObject

Returns the value of attribute current_fieldset.



28
29
30
# File 'lib/lolita/configuration/tab.rb', line 28

def current_fieldset
  @current_fieldset
end

#current_nested_formObject

Returns the value of attribute current_nested_form.



28
29
30
# File 'lib/lolita/configuration/tab.rb', line 28

def current_nested_form
  @current_nested_form
end

#dbiObject

Returns the value of attribute dbi.



28
29
30
# File 'lib/lolita/configuration/tab.rb', line 28

def dbi
  @dbi
end

#field_setsObject (readonly)

Returns the value of attribute field_sets.



29
30
31
# File 'lib/lolita/configuration/tab.rb', line 29

def field_sets
  @field_sets
end

#nested_formsObject (readonly)

Returns the value of attribute nested_forms.



29
30
31
# File 'lib/lolita/configuration/tab.rb', line 29

def nested_forms
  @nested_forms
end

Instance Method Details

#default_fieldsObject

Create fields for tab from database. See Lolita::Adapter classes for use of DB field method.



85
86
87
88
89
# File 'lib/lolita/configuration/tab.rb', line 85

def default_fields
  self.current_dbi.fields.each{|db_field|
    self.field(:name => db_field.name, :type => db_field.type, :dbi_field => db_field) if db_field.content?
  }
end

#field(*args, &block) ⇒ Object

Field setter method, accpet *args and &block to be passed. For details how to pass args and block see Lolita::Configuration::Field. Return field itself.



51
52
53
54
55
56
57
58
59
60
# File 'lib/lolita/configuration/tab.rb', line 51

def field *args, &block
  field=Lolita::Configuration::Factory::Field.add(self.current_dbi,*args,&block)
  field.field_set = current_fieldset
  if self.current_dbi!=self.dbi
    field.nested_in=self.dbi
    field.nested_form = current_nested_form
  end
  @fields << field
  field
end

#field_set(name, &block) ⇒ Object

Create new field_set for current tab with given name and &block that will be evaluted in current tab instance.



114
115
116
117
118
119
120
# File 'lib/lolita/configuration/tab.rb', line 114

def field_set name,&block
  field_set=Lolita::Configuration::FieldSet.new(self,name)
  self.current_fieldset = field_set
  @field_sets << field_set
  self.instance_eval(&block)
  self.current_fieldset=nil
end

#fieldsObject

Return all fields in tab.



63
64
65
# File 'lib/lolita/configuration/tab.rb', line 63

def fields
  @fields 
end

#fields=(fields) ⇒ Object

Set all fields in tab. Accept fields as Array. Each array element can be Lolita::Configuration::Field object or Hash, that will be passed to #field method.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lolita/configuration/tab.rb', line 70

def fields=(fields)
  @fields=Lolita::Configuration::Fields.new
  if fields.is_a?(Array)
    fields.each{|field_attr|
      if field_attr.is_a?(Lolita::Configuration::Field)
        @fields<<field_attr
      else
        self.field(field_attr)
      end
    }
  end
end

#fields_in_groupsObject

Return fields in groups where in one group are fields for same model. It return all groups as array or yield each group when block is given.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lolita/configuration/tab.rb', line 138

def fields_in_groups()
  groups = []
  current_class = nil
  self.fields.each do |group_field|

    klass = group_field.dbi.klass
    if current_class == klass
      groups.last << group_field
    else
      groups << [group_field]
    end
    current_class = klass
  end
  if block_given?
    groups.each{|group| yield group }
  else
    groups
  end
end

#fields_with_field_setObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lolita/configuration/tab.rb', line 122

def fields_with_field_set
  used_fieldsets=[]
  self.fields.each{|field|
    if !field.field_set || (!used_fieldsets.include?(field.field_set))
      if field.field_set
        yield field.field_set.fields,field.field_set
        used_fieldsets<<field.field_set
      else
        yield field,nil
      end
    end
  }
end

#nested_fields_for(class_or_name, options = {}, &block) ⇒ Object

Add tab nested fields for class_or_name and &block that will be evaluted in current tab instance.



93
94
95
96
97
98
99
100
101
102
# File 'lib/lolita/configuration/tab.rb', line 93

def nested_fields_for class_or_name, options ={},&block
  current_class = get_class(class_or_name)
  nested_form = Lolita::Configuration::NestedForm.new(self,class_or_name, options)
  self.current_nested_form = nested_form
  @nested_forms << nested_form
  self.current_dbi = Lolita::DBI::Base.create(current_class)
  self.instance_eval(&block)
  self.current_dbi = self.dbi
  self.current_nested_form = nil
end

#nested_fields_of(class_or_name) ⇒ Object

Return nested field for given class_or_name



105
106
107
108
109
110
# File 'lib/lolita/configuration/tab.rb', line 105

def nested_fields_of class_or_name
  current_class=get_class(class_or_name)
  self.fields.select{|field|
    field.nested_in?(@dbi) && field.dbi.klass==current_class
  }
end

#set_attributes(*args) ⇒ Object

Set attributes from given *args. First element of args is used as type other interpreted as options. Every Hash key is used as setter method, and value as method value.

Example

set_attributes(:content,:field=>{:name=>"My Field"})
set_attributes(:field=>{:name=>"My Field"})


164
165
166
167
168
169
170
171
172
# File 'lib/lolita/configuration/tab.rb', line 164

def set_attributes *args
  if args
    options=args.extract_options!
    options.each{|method,options|
      self.send(:"#{method}=",options)
    }
  end
  
end

#title(new_title = nil) ⇒ Object



174
175
176
177
178
179
# File 'lib/lolita/configuration/tab.rb', line 174

def title new_title = nil
  if new_title
    @title = new_title
  end
  Lolita::Utils.dynamic_string(@title, :default => @type.to_s.humanize)
end