Class: MDWA::Generators::ModelAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/mdwa/generators/model_attribute.rb

Constant Summary collapse

STATIC_TYPES =
[:boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp, :file, :password, :status]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ ModelAttribute

Sets the attributes variables Format: <name>:<type>,<model>:<reference>:<reference_type>



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mdwa/generators/model_attribute.rb', line 14

def initialize( arg )

	# sets the variables by the string
	split = arg.split(':')
	self.name = split[0]
	self.reference = split[2]
	self.reference_type = split[3]
	self.type = split[1] # type is the most important, so it's the last for override reasons
	
	@raw_string = arg
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



8
9
10
# File 'lib/mdwa/generators/model_attribute.rb', line 8

def model
  @model
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/mdwa/generators/model_attribute.rb', line 8

def name
  @name
end

#referenceObject

Returns the value of attribute reference.



8
9
10
# File 'lib/mdwa/generators/model_attribute.rb', line 8

def reference
  @reference
end

#reference_typeObject

Returns the value of attribute reference_type.



8
9
10
# File 'lib/mdwa/generators/model_attribute.rb', line 8

def reference_type
  @reference_type
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/mdwa/generators/model_attribute.rb', line 8

def type
  @type
end

Instance Method Details

#belongs_to?Boolean



108
109
110
# File 'lib/mdwa/generators/model_attribute.rb', line 108

def belongs_to?
	return (reference_type == 'belongs_to')
end

#filter_inputObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mdwa/generators/model_attribute.rb', line 85

def filter_input
	input = []
     case self.type.to_s.to_sym
       when :text, :string then
       	input << "text_field_tag :#{self.name}"
      	when :integer, :float, :decimal then
       	input << "text_field_tag :#{self.name}, '', :onkeypress => \"mascara(this, checaNumero)\", :maxlength => 10"
       when :file, :password then 
       	input << 'No possible filter for file or password.'
       when :time then 
       	input << 'time_select'
       when :datetime, :timestamp, :date then 
       	input << "text_field_tag :#{self.name}_0, '', :class => :datepicker, :onkeypress => \"mascara(this, checaData)\", :maxlength => 10"
       	input << "text_field_tag :#{self.name}_1, '', :class => :datepicker, :onkeypress => \"mascara(this, checaData)\", :maxlength => 10"
       when :boolean then
         input << "select_tag :#{self.name}, options_for_select([[t('system.yes'), 1], [t('system.no'), 0]]), :prompt => t('system.both')"
       when :status then
         input << "select_tag :#{self.name}, options_for_select(#{self.model.klass}.#{self.name}_to_select), :prompt => t('#{self.model.plural_name}.#{self.name}.prompt_select')"
       else
       	input << "text_field_tag :#{self.name}"
     end
end

#form_fieldObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mdwa/generators/model_attribute.rb', line 69

def form_field
     @form_field ||= case self.type.to_s.to_sym
       when :integer              then 'number_field'
       when :float, :decimal      then 'text_field'
       when :file				         then 'file_field'
       when :time                 then 'time_select'
       when :datetime, :timestamp then 'datetime_select'
       when :date                 then 'text_field'
       when :text                 then 'text_area'
       when :boolean              then 'check_box'
       when :password             then 'password_field'  
       else
         'text_field'
     end
end

#has_and_belongs_to_many?Boolean



132
133
134
# File 'lib/mdwa/generators/model_attribute.rb', line 132

def has_and_belongs_to_many?
	return (reference_type == 'has_and_belongs_and_to_many')
end

#has_many?Boolean



112
113
114
# File 'lib/mdwa/generators/model_attribute.rb', line 112

def has_many?
	return (reference_type == 'has_many')
end

#has_one?Boolean



128
129
130
# File 'lib/mdwa/generators/model_attribute.rb', line 128

def has_one?
	return (reference_type == 'has_one')
end

#migration_fieldObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mdwa/generators/model_attribute.rb', line 53

def migration_field
	@migration_field ||= case self.type.to_s.to_sym
       when :string, :password         then 'string'
       when :boolean                   then 'boolean'
       when :date 				              then 'date'
       when :datetime                  then 'datetime'
       when :decimal, :float           then 'decimal'
       when :text 				              then 'text'
       when :time 				              then 'time'
       when :timestamp 			          then 'timestamp'
       when :file                      then 'attachment'
       else
         'integer'
     end
end

#nested?Boolean



120
121
122
# File 'lib/mdwa/generators/model_attribute.rb', line 120

def nested?
	nested_many? || nested_one?
end

#nested_many?Boolean



116
117
118
# File 'lib/mdwa/generators/model_attribute.rb', line 116

def nested_many?
	return (reference_type == 'nested_many')
end

#nested_one?Boolean



124
125
126
# File 'lib/mdwa/generators/model_attribute.rb', line 124

def nested_one?
	return (reference_type == 'nested_one')
end

#rawObject



26
27
28
# File 'lib/mdwa/generators/model_attribute.rb', line 26

def raw
 @raw_string
end

#references?Boolean



136
137
138
# File 'lib/mdwa/generators/model_attribute.rb', line 136

def references?
	!STATIC_TYPES.include?(self.type.to_s.to_sym)
end