Class: ActiveWorkbench::FK

Inherits:
Object
  • Object
show all
Defined in:
lib/active_workbench/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, id, name, many = false, assoc = :belongs) ⇒ FK

Returns a new instance of FK.



108
109
110
111
112
113
114
# File 'lib/active_workbench/base.rb', line 108

def initialize table, id, name, many = false, assoc = :belongs
  @table = table
  @id = id
  @name = name
  @many = many
  @assoc = assoc
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



105
106
107
# File 'lib/active_workbench/base.rb', line 105

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



105
106
107
# File 'lib/active_workbench/base.rb', line 105

def name
  @name
end

#ref_tableObject

Returns the value of attribute ref_table.



106
107
108
# File 'lib/active_workbench/base.rb', line 106

def ref_table
  @ref_table
end

Instance Method Details

#add_column(col) ⇒ Object



128
129
130
131
# File 'lib/active_workbench/base.rb', line 128

def add_column(col)
  @columns ||= []
  @columns << col
end

#add_ref_column(col) ⇒ Object



133
134
135
136
# File 'lib/active_workbench/base.rb', line 133

def add_ref_column(col)
  @ref_columns ||= []
  @ref_columns << col
end

#columnsObject



120
121
122
# File 'lib/active_workbench/base.rb', line 120

def columns
  @columns
end

#is_many?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/active_workbench/base.rb', line 116

def is_many?
  @many == true
end

#ref_columnsObject



124
125
126
# File 'lib/active_workbench/base.rb', line 124

def ref_columns
  @ref_columns
end

#to_assotiationObject



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
172
173
174
175
176
177
178
179
180
181
# File 'lib/active_workbench/base.rb', line 138

def to_assotiation
  # belongs_to assotiation
  if @assoc == :belongs
    pkey = ""
    if @ref_columns.size > 1
    else
      #@ref_columns.collect { |col| "#{col.table.name}.#{col.name}

      if @ref_columns.first.name != 'id'
        pkey = ", :primary_key = \"#{@ref_columns.first.name}\""
      end
    end

    fkey = ""
    if @columns.size > 1
    else
      #@ref_columns.collect { |col| "#{col.table.name}.#{col.name}
      if @columns.first.name != ref_table.class_name.tableize.singularize.foreign_key
        fkey = ", :foreign_key => \"#{@columns.first.name}\""
      end
    end
    "\n  belongs_to :#{ref_table.class_name.tableize.singularize}#{ref_table.plural? ? "" : ", :class_name => \"#{ref_table.class_name}\""}#{fkey}#{pkey}" # :#{@columns.collect { |col| "#{col.table.name}.#{col.name}" }} >> :#{@ref_columns.collect { |col| "#{col.table.name}.#{col.name}" }}"
  else

    pkey = ""
    if @columns.size > 1
    else
      #@ref_columns.collect { |col| "#{col.table.name}.#{col.name}
      if @columns.first.name != 'id'
        pkey = ", :primary_key = \"#{@ref_columns.first.name}\""
      end
    end

    fkey = ""
    if @ref_columns.size > 1
    else
      if @ref_columns.first.name != @table.class_name.tableize.singularize.foreign_key
        fkey = ", :foreign_key => \"#{@ref_columns.first.name}\""
      end
    end

    "\n  #{@many ? "has_many :" + ref_table.class_name.tableize : "has_one :" + ref_table.class_name.tableize.singularize}#{ref_table.plural? ? "" : ", :class_name => \"#{ref_table.class_name}\""}#{fkey}#{pkey}" # :#{@columns.collect { |col| "#{col.table.name}.#{col.name}" }} >> :#{@ref_columns.collect { |col| "#{col.table.name}.#{col.name}" }}"
  end
end