Module: Stuffer

Defined in:
lib/stuffer.rb,
lib/stuffer/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#capy_fieldsObject (readonly)

Returns the value of attribute capy_fields.



6
7
8
# File 'lib/stuffer.rb', line 6

def capy_fields
  @capy_fields
end

#factory_fieldsObject (readonly)

Returns the value of attribute factory_fields.



6
7
8
# File 'lib/stuffer.rb', line 6

def factory_fields
  @factory_fields
end

Class Method Details

.check(checkbox) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/stuffer.rb', line 98

def self.check checkbox
  if checkbox.is_a? Symbol
    checkbox = convert checkbox
  end
  if @id.include?(checkbox) && @capy_fields[checkbox] == 'input'
    Capybara.check checkbox
  end
end

.convert(symbol) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stuffer.rb', line 60

def self.convert symbol
  if !@nested_fields
    symbol_field = "#{@factory_convert+symbol.to_s}"
    if @fields.keys.include?(symbol_field)
      symbol = symbol_field
      symbol
    end
  else
    symbol_field = "#{@factory_convert+symbol.to_s}"
    symbol_nest = "#{@factory_convert+@nest.to_s}_attributes_#{symbol.to_s}"
    if @fields.keys.include?(symbol_field)
      symbol = symbol_field
      symbol
    elsif @fields.keys.include?(symbol_nest)
      symbol = symbol_nest
      symbol
    end
  end
end

.factorize(factory, nest_factory = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/stuffer.rb', line 8

def self.factorize (factory, nest_factory=nil)
  @factory = factory
  @id = []
  @factory_convert = "#{@factory.to_s.downcase}_"
  @factory_fields = FactoryGirl.attributes_for(factory)
  @factory_fields.keys.each do |key|
    @id << "#{@factory_convert + key.to_s}"
  end
  if nest_factory
    get_nested nest_factory
  end
  super_field
  get_fields
end

.fill(name) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/stuffer.rb', line 80

def self.fill name
  if name.is_a? Symbol
    name = convert name
  end
  if @id.include?(name) && @capy_fields[name] == 'input'
    Capybara.fill_in name, with: @fields[name]
  end
end

.get_fieldsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/stuffer.rb', line 45

def self.get_fields
  @capy_fields = {}
  Capybara.visit Capybara.current_url
  @id.each do |field|
    begin
      Capybara.page.find_field(field)
    rescue
      false
    else
      @capy_fields[field] = Capybara.page.find_field(field).tag_name
    end
  end
  @capy_fields
end

.get_nested(nest) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/stuffer.rb', line 37

def self.get_nested nest
  @nest = nest
  @nested_fields = FactoryGirl.attributes_for(nest)
  @nested_fields.keys.each do |key|
    @id << "#{@factory_convert+nest.to_s}_attributes_#{key.to_s}"
  end
end

.radio(value) ⇒ Object



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

def self.radio value
  if value.is_a? Symbol
    value = convert value
  end
  if @id.include? value
    Capybara.choose("#{value}_#{@fields[value]}")
  end
end

.select(selection) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/stuffer.rb', line 89

def self.select selection
  if selection.is_a? Symbol
    selection = convert selection
  end
  if @id.include?(selection) && @capy_fields[selection] == 'select'
    Capybara.select @fields[selection], from: selection
  end
end

.stuffObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/stuffer.rb', line 116

def self.stuff
  @id.each do |stuff|
    if @capy_fields[stuff] == 'input' && @fields[stuff] != false && @fields[stuff] != true
      fill stuff
    elsif @capy_fields[stuff] == 'input'
      check stuff
    elsif @capy_fields[stuff] == 'select'
      select stuff
    else
      begin
        radio stuff
      rescue
        false
      else
        true
      end
    end
  end
end

.super_fieldObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stuffer.rb', line 23

def self.super_field
  @fields = {}
  counter = 0
  if @nested_fields
    @stuffer = @factory_fields.merge(@nested_fields)
  else
    @stuffer = @factory_fields
  end
  @stuffer.values.each do |value|
    @fields[@id[counter]] = value
    counter += 1
  end
end