Module: RecordHelper

Defined in:
lib/common_steps/helpers/record_helper.rb

Instance Method Summary collapse

Instance Method Details

#conditions_from_str(conditions_str) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/common_steps/helpers/record_helper.rb', line 84

def conditions_from_str(conditions_str)
  record_conds = conditions_str.gsub(", and", ",").gsub(" and", ",").split(", ")
  conds = record_conds.map {|rc| rc.gsub(" => ", " of ").split(" of ") }

#    conds.inject({}) do |base, (attr_name, value_str)|
#      base[attr_from_name(class_record, attr_name)] = value_from_str(value_str)
#      base
#    end
  conds.inject({}) {|base, (attr, value_str)| base[attr] = instance_eval(value_str); base}
end

#find_record(record_class, record_conditions) ⇒ Object



95
96
97
98
99
# File 'lib/common_steps/helpers/record_helper.rb', line 95

def find_record(record_class, record_conditions)
  conditions = conditions_from_str(record_conditions)
  record = record_class.find(:first, :conditions => conditions)
  record.nil? ? raise("Couldn't found any record for `#{record_class}' with conditions: `#{conditions.inspect}'") : record
end

#record_class_to_path(record_class) ⇒ Object



101
102
103
# File 'lib/common_steps/helpers/record_helper.rb', line 101

def record_class_to_path(record_class)
  "/#{record_class.name.underscore.pluralize}"
end

#record_name_to_class(record_name) ⇒ Object



6
7
8
9
10
# File 'lib/common_steps/helpers/record_helper.rb', line 6

def record_name_to_class(record_name)
  klass_name = record_singular_name(record_name).gsub(/\s/, "_").classify
  klass = klass_name.constantize
  klass.nil? ? raise("Couldn't found any class for record with name `#{record_name}', tried with `#{klass_name}'") : klass
end

#record_singular_name(record_name) ⇒ Object



2
3
4
# File 'lib/common_steps/helpers/record_helper.rb', line 2

def record_singular_name(record_name)
  record_name.gsub(' ', '_').downcase.singularize
end

#recordize!(record_name, table, &block) ⇒ Object

leo = Artist.create!(:name => “Leo”) Paiting.belongs_to :artist Paintings: | title | artist_name | | Mona | Leo | will associate leo to the paiting with title Mona



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/common_steps/helpers/record_helper.rb', line 19

def recordize!(record_name, table, &block)
  record_class = record_name_to_class(record_name)
  table.headers.each do |header|
    rh = record_class.columns_hash[header]
    if rh.nil? # artist_name case 
      association = record_class.reflect_on_all_associations.detect {|a| header =~ /^#{a.name}/ }
      raise("Association: `#{header}' not found on #{record_class}") if association.nil?
      
      find_attrs = header.match(/^#{association.name}(.*)$/)[1]
      association_class = association.klass
      table.map_column!(header) { |value| association_class.send("find_by#{find_attrs}", value) }
      table.map_headers!(header => association.name)
    else
      table.map_column!(header) {|value| rh.type_cast(value) }
    end
  end
end

#str_to_num(str) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/common_steps/helpers/record_helper.rb', line 37

def str_to_num(str)
  case str
  when "a" then 1
  when "an" then 1
  when "no" then 0
  else str.to_i
  end
end

#value_from_str(value_str) ⇒ Object

def attr_from_name(class_record, attr_name)

  case attr_name
  when /^belonging to (a|an) (\w+) with (a|an) (.*)$/
    _, association_str, _, conditions_str = $~.captures
    association_name = association_str.gsub(/\s/, "_").downcase.to_sym
    association = reflect_on_association(association_name)

    raise("Association `#{association_name}' not found on #{record_class}") if association.nil?
    raise("Association `#{association_name}' isn't belongs_to(is a #{association.macro})") unless association.belongs_to?

    association
    association_record = find_record(association.klass, conditions_str)

  else
    attr_name
  end

  if record_class.columns_hash[attr_name] # record_class.has_column?(attr_name)
  elsif
  end
  if column_attr.nil? # artist_name case 
    association = record_class.reflect_on_all_associations.detect {|a| header =~ /^#{a.name}/ }
    raise("Association: `#{header}' not found on #{record_class}") if association.nil?

    find_attrs = header.match(/^#{association.name}(.*)$/)[1]
    association_class = association.klass
    table.map_column!(header) { |value| association_class.send("find_by#{find_attrs}", value) }
  else
  end
end


80
81
82
# File 'lib/common_steps/helpers/record_helper.rb', line 80

def value_from_str(value_str)
  instance_eval(value_str)
end