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

@@default_tab_type =

For different types there are different builders(cells)

:default
@@available_types =
[:content]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder

#build, #builder_options, #default_builder, #get_builder

Constructor Details

#initialize(dbi, *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.



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

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

Instance Attribute Details

#current_dbiObject

Returns the value of attribute current_dbi.



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

def current_dbi
  @current_dbi
end

#current_fieldsetObject

Returns the value of attribute current_fieldset.



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

def current_fieldset
  @current_fieldset
end

#dbiObject

Returns the value of attribute dbi.



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

def dbi
  @dbi
end

#field_setsObject (readonly)

Returns the value of attribute field_sets.



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

def field_sets
  @field_sets
end

#nested_formObject (readonly)

Returns the value of attribute nested_form.



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

def nested_form
  @nested_form
end

Class Method Details

.default_typesObject



173
174
175
# File 'lib/lolita/configuration/tab.rb', line 173

def default_types
  @@available_types
end

Instance Method Details

#default_fieldsObject

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



83
84
85
86
87
# File 'lib/lolita/configuration/tab.rb', line 83

def default_fields
  self.current_dbi.fields.each{|db_field|
    self.field(db_field)
  }
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.



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

def field *args, &block
  field=Lolita::Configuration::Field.add(self.current_dbi,*args,&block)
  field.field_set=current_fieldset
  if self.current_dbi!=self.dbi
    field.nested_in=self.dbi
  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.



108
109
110
111
112
113
114
# File 'lib/lolita/configuration/tab.rb', line 108

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.



61
62
63
# File 'lib/lolita/configuration/tab.rb', line 61

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.



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

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_with_field_setObject



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

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, &block) ⇒ Object

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



91
92
93
94
95
96
# File 'lib/lolita/configuration/tab.rb', line 91

def nested_fields_for class_or_name,&block
  current_class=get_class(class_or_name)
  self.current_dbi=Lolita::DBI::Base.new(current_class)
  self.instance_eval(&block)
  self.current_dbi=self.dbi
end

#nested_fields_of(class_or_name) ⇒ Object

Return nested field for given class_or_name



99
100
101
102
103
104
# File 'lib/lolita/configuration/tab.rb', line 99

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"})


141
142
143
144
145
146
147
148
149
150
# File 'lib/lolita/configuration/tab.rb', line 141

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