class Lolita::Configuration::Tab::Base
Attributes
Public Class Methods
To create new tab the following parametrs need to be provided.
-
dbi
Lolita::DBI::Base
object, that represents database. -
*args
Seeset_attributes
, for how these args are processed. -
&block
Block can be passed, anything in block will be evaled for current instance.
# 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
Public Instance Methods
Create fields for tab from database. See Lolita::Adapter
classes for use of DB field method.
# 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
setter method, accpet *args and &block to be passed. For details how to pass args and block see Lolita::Configuration::Field
. Return field itself.
# 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
Create new field_set
for current tab with given name and &block that will be evaluted in current tab instance.
# 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
Return all fields in tab.
# File lib/lolita/configuration/tab.rb, line 63 def fields @fields end
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.
# 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
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.
# 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
# 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
Add tab nested fields for class_or_name and &block that will be evaluted in current tab instance.
# 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
Return nested field for given class_or_name
# 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 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"})
# 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
# 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
Private Instance Methods
# File lib/lolita/configuration/tab.rb, line 212 def builder_local_variable_name :tab end
# File lib/lolita/configuration/tab.rb, line 201 def get_class(association_name) if association_name.is_a?(Symbol) && assoc = self.current_dbi.reflect_on_association(association_name) if dbi.nested_attributes_options[association_name].nil? && dbi.nested_attributes_options[association_name.to_s].nil? raise ArgumentError, "#{dbi.klass} association named `#{association_name}` called nested_fields_XXX without accepts_nested_attributes_for :#{association_name}" end assoc.klass else raise ArgumentError, "Association named `#{association_name}` not found for #{self.current_dbi.klass}." end end
# File lib/lolita/configuration/tab.rb, line 183 def my_type self.class.to_s.split("::").last.downcase.to_sym end
# File lib/lolita/configuration/tab.rb, line 187 def set_default_attributes @name = "tab_#{self.__id__}" unless @name @title = set_default_title unless @title end
# File lib/lolita/configuration/tab.rb, line 192 def set_default_title if defined?(::I18n) translation_str = "lolita.tabs.titles.#{@type}" Proc.new{ ::I18n.t(translation_str) } else @type.to_s.humanize end end