2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/generators/voluntary/install/templates/features/step_definitions/factory_steps.rb', line 2
def create_from_table(model_name, table, = {})
factory_name = model_name.gsub(/\W+/, '_').downcase.singularize.to_sym
is_singular = model_name.to_s.singularize == model_name.to_s
hashes = if is_singular
if table.kind_of?(Hash)
[table]
else
[table.rows_hash]
end
elsif table.is_a?(Array)
table
else
table.hashes
end
@klass = factory_name.to_s.classify.constantize
@they = hashes.map do |hash|
hash = hash.merge().inject({}) do |h,(k,v)|
k = k.gsub(/\W+/,'_')
if @klass.respond_to?(:serialized_attributes) && @klass.serialized_attributes[k] == Array
v = v.split(/\s*,\s*/)
end
h.update(k.to_sym => v)
end
hash.keys.each do |attribute|
set_value(hash, attribute)
end
eval("set_#{factory_name}_defaults(hash)") if "#{factory_name.to_s.classify}FactoryMethods".constantize rescue nil
object = nil
object = @klass.where(name: hash[:name]).first if hash.has_key? :name
object = object ? object : Factory.build(factory_name, hash)
yield object if block_given?
object.save!
object
end
if is_singular
@it = @they.last
instance_variable_set("@#{factory_name}", @it)
end
end
|