Module: Inputex::ModelExtensions::ClassMethods

Defined in:
lib/inputex.rb

Overview

“extend ClassMethods” is automatically executed at module’s inclusion (thanks to ActiveSupport::Concern)

Instance Method Summary collapse

Instance Method Details

#inputex_field_byname(columnName, force_uneditable = false) ⇒ Object

Return the inputEx definition from a column name



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/inputex.rb', line 72

def inputex_field_byname(columnName, force_uneditable = false)
  column = self.columns_hash[columnName]
  
  # Support for attribute accessors
  if column.nil?
    column = FakeColumn.new
    column.name = columnName
  end
  
  inputex_field_definition(column, force_uneditable)
end

#inputex_field_definition(column, force_uneditable = false) ⇒ Object

Generate an inputEx definition from the column object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/inputex.rb', line 85

def inputex_field_definition(column, force_uneditable = false)
  
  field = {
    :type => "string",
    :label => (I18n.t "activerecord.attributes.#{self.name.underscore}.#{column.name}").capitalize,
    :name => column.name,
    :showMsg => true
  }
  
  # Field type
  # TODO: utiliser les types rails
  if force_uneditable
    field[:type] = "uneditable"
    
  elsif column.name == "created_at" || column.name == "updated_at"
    field[:type] = "uneditable"
    
  elsif column.name == "id"
    field[:type] = "hidden"
    
  elsif column.name == "email"
    field[:type] = "email"
    
  elsif column.name == "password" || column.name == "hashed_password"
    field[:type] = "password"
    
  elsif column.name == "colorref"
    field[:type] = "color"
    
  elsif column.name == "ip"
    field[:type] = "IPv4"
    
  elsif column.name == "url"
    field[:type] = "url"
    
  elsif column.name[-3..-1] == "_id" # Autocompleters for liaisons
    # The name of the attribute might be different than the name of the class
    # (ex: Leaf.parent_id refering to another Leaf, the inputEx type is still "leaf_id")
    liaisonName = (column.name[0..-4]).to_sym
    reflection = self.reflections[ liaisonName.to_sym ]
    if reflection
      field[:type] = reflection.klass.to_s.downcase+"_id"
    else
      field[:type] = column.name
    end
    
  elsif column.sql_type == "datetime"
    field[:type] = "datetime"
    
  elsif column.sql_type == "tinyint(1)"
    field[:type] =  "boolean"
    
  elsif column.sql_type == "int(11)"
    field[:type] = "integer"
    
  elsif column.sql_type == "text"
    field[:type] = "text"
    field[:rows] = 6
    field[:cols] = 70
    
  else
    
    # test presence of an InclusionValidator
    inclusion_validators = self.validators_on(column.name).select { |validator|
      validator.is_a?(ActiveModel::Validations::InclusionValidator)
    }
    
    if inclusion_validators.size > 0
      field[:type]    = "select"
      field[:choices] = inclusion_validators[0].send(:delimiter) # use send cause delimiter is private
    end
     
  end
  
  # Default value
  if !column.default.nil?
    field[:value] = column.default
  end
  
  # Required ?
  #if !column.null
  #  field[:required] = true
  #end
  
  field
  
end

#inputex_fields_definition(columnNames = self.columns_hash.keys) ⇒ Object

Return a list of inputEx definitions for the given column names



65
66
67
68
69
# File 'lib/inputex.rb', line 65

def inputex_fields_definition(columnNames = self.columns_hash.keys)
  columnNames.map { |c|
    inputex_field_byname(c)
  }
end