Class: UiChanged::Screenshot

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/ui_changed/screenshot.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.async_crawl_and_compareObject



77
78
79
80
81
# File 'app/models/ui_changed/screenshot.rb', line 77

def async_crawl_and_compare
  start_async_crawl_for_control
  start_async_crawl_for_test
  start_async_compare
end

.delete_all_comparesObject



130
131
132
133
# File 'app/models/ui_changed/screenshot.rb', line 130

def delete_all_compares
  UiChanged::Screenshot.delete_all(:is_compare => true)
  FileUtils.rm_rf(Dir.glob(UiChanged::ConfigHelper.compare_path + '*'))
end

.delete_all_controlsObject

delete all entries & images



118
119
120
121
122
123
# File 'app/models/ui_changed/screenshot.rb', line 118

def delete_all_controls
  UiChanged::Screenshot.delete_all(:is_control => true)
  UiChanged::Screenshot.delete_all(:is_compare => true)
  FileUtils.rm_rf(Dir.glob(UiChanged::ConfigHelper.control_path + '*'))
  FileUtils.rm_rf(Dir.glob(UiChanged::ConfigHelper.compare_path + '*'))
end

.delete_all_testsObject



124
125
126
127
128
129
# File 'app/models/ui_changed/screenshot.rb', line 124

def delete_all_tests
  UiChanged::Screenshot.delete_all(:is_test => true)
  UiChanged::Screenshot.delete_all(:is_compare => true)
  FileUtils.rm_rf(Dir.glob(UiChanged::ConfigHelper.test_path + '*'))
  FileUtils.rm_rf(Dir.glob(UiChanged::ConfigHelper.compare_path + '*'))
end

.destroy_entries_and_images(ids) ⇒ Object



135
136
137
138
139
# File 'app/models/ui_changed/screenshot.rb', line 135

def destroy_entries_and_images(ids)
  ids.each do |id|
    destroy_entry_and_image(id)
  end
end

.destroy_entry_and_image(id) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/ui_changed/screenshot.rb', line 140

def destroy_entry_and_image(id)
  ss = UiChanged::Screenshot.find(id)

  # remove the corresponding compare screenshot
  if ss.is_control
    compare_ss = UiChanged::Screenshot.find_by_control_id(ss.id)
    if compare_ss
      compare_ss.remove_image
      compare_ss.destroy
    end
  elsif ss.is_test
    compare_ss = UiChanged::Screenshot.find_by_test_id(ss.id)
    if compare_ss
      compare_ss.remove_image
      compare_ss.destroy
    end
  end

  ss.remove_image
  ss.destroy
end

.move_all_test_images_to_controlObject



181
182
183
184
185
186
187
188
189
190
191
# File 'app/models/ui_changed/screenshot.rb', line 181

def move_all_test_images_to_control
  # delete all control images
  FileUtils.rm_rf(Dir.glob(UiChanged::ConfigHelper.control_path + '*'))

  # move all test images to control directory
  Dir.foreach(UiChanged::ConfigHelper.test_path) do |img|
    next if img == '.' || img == '..'
    # do work on real items
    UiChanged::Screenshot.move_test_image_to_control(img)
  end
end

.move_test_image_to_control(image_file_name_full) ⇒ Object



193
194
195
196
# File 'app/models/ui_changed/screenshot.rb', line 193

def move_test_image_to_control(image_file_name_full)
  FileUtils.mv(UiChanged::ConfigHelper.test_path + image_file_name_full,
               UiChanged::ConfigHelper.control_path + image_file_name_full)
end

.not_in_ignoredObject



198
199
200
# File 'app/models/ui_changed/screenshot.rb', line 198

def not_in_ignored
  UiChanged::Screenshot.joins("LEFT JOIN ui_changed_screenshot_ignore_urls siu ON siu.url = ui_changed_screenshots.url").where('siu.id is null')
end

.remove_all_diffs_and_testsObject



162
163
164
165
# File 'app/models/ui_changed/screenshot.rb', line 162

def remove_all_diffs_and_tests
  delete_all_compares
  delete_all_tests
end

.remove_diff_and_test(diff_id) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'app/models/ui_changed/screenshot.rb', line 171

def remove_diff_and_test(diff_id)
  ss_diff = UiChanged::Screenshot.find(diff_id)
  ss_test = UiChanged::Screenshot.find(ss_diff.test_id)
  ss_diff.remove_image
  ss_diff.destroy

  ss_test.remove_image
  ss_test.destroy
end

.remove_diffs_and_tests(diff_ids) ⇒ Object



166
167
168
169
170
# File 'app/models/ui_changed/screenshot.rb', line 166

def remove_diffs_and_tests(diff_ids)
  diff_ids.split(",").each do |diff_id|
    remove_diff_and_test(diff_id.first.to_i)
  end
end

.search(search) ⇒ Object



83
84
85
# File 'app/models/ui_changed/screenshot.rb', line 83

def search(search)
  UiChanged::Screenshot.where('ui_changed_screenshots.url LIKE ?', "%#{search}%")
end

.set_all_tests_as_controlsObject



86
87
88
89
90
# File 'app/models/ui_changed/screenshot.rb', line 86

def set_all_tests_as_controls
  delete_all_controls
  move_all_test_images_to_control
  UiChanged::Screenshot.update_all({:is_control => true, :is_test => false}, {:is_test => true})
end

.set_test_as_control(test_id) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/ui_changed/screenshot.rb', line 96

def set_test_as_control(test_id)
  test_ss = UiChanged::Screenshot.find(test_id)
  control_ss = UiChanged::Screenshot.find_by_url_and_is_control(test_ss.url, true)
  compare_ss = UiChanged::Screenshot.find_by_test_id(test_ss.id)

  unless !control_ss
    control_ss.remove_image
    control_ss.destroy
  end
  unless !compare_ss
    compare_ss.remove_image
    compare_ss.destroy
  end

  test_ss.is_control = true
  test_ss.is_test = false
  test_ss.save
  # move test ss's to control dir
  test_ss.move_test_image_to_control
end

.set_tests_as_controls(test_ids) ⇒ Object



91
92
93
94
95
# File 'app/models/ui_changed/screenshot.rb', line 91

def set_tests_as_controls(test_ids)
  test_ids.each do |test_id|
    set_test_as_control(test_id)
  end
end

.start_async_compareObject



73
74
75
# File 'app/models/ui_changed/screenshot.rb', line 73

def start_async_compare
  UiChanged::Compare.create
end

.start_async_crawl_for_controlObject



54
55
56
# File 'app/models/ui_changed/screenshot.rb', line 54

def start_async_crawl_for_control
  UiChanged::CrawlControl.create
end

.start_async_crawl_for_control_and_compareObject



65
66
67
68
# File 'app/models/ui_changed/screenshot.rb', line 65

def start_async_crawl_for_control_and_compare
  start_async_crawl_for_control
  start_async_compare
end

.start_async_crawl_for_control_and_testObject



61
62
63
64
# File 'app/models/ui_changed/screenshot.rb', line 61

def start_async_crawl_for_control_and_test
  start_async_crawl_for_control
  start_async_crawl_for_test
end

.start_async_crawl_for_testObject



58
59
60
# File 'app/models/ui_changed/screenshot.rb', line 58

def start_async_crawl_for_test
  UiChanged::CrawlTest.create
end

.start_async_crawl_for_test_and_compareObject



69
70
71
72
# File 'app/models/ui_changed/screenshot.rb', line 69

def start_async_crawl_for_test_and_compare
  start_async_crawl_for_test
  start_async_compare
end

Instance Method Details

#as_json(*a) ⇒ Object



253
254
255
# File 'app/models/ui_changed/screenshot.rb', line 253

def as_json(*a)
  self.attributes.merge({"displayable_image_path_full" => self.displayable_image_path_full})
end

#check_compare_idsObject



41
42
43
44
45
# File 'app/models/ui_changed/screenshot.rb', line 41

def check_compare_ids
  if is_compare && (control_id == nil || test_id == nil)
    errors[:base] << 'is_compare screenshots must have their control_id and test_id set'
  end
end

#check_diff_foundObject



36
37
38
39
40
# File 'app/models/ui_changed/screenshot.rb', line 36

def check_diff_found
  if diff_found && !( !is_control && !is_test && is_compare )
    errors[:base] << 'when diff found is set it must be of type is_compare'
  end
end

#check_image_content_typeObject



46
47
48
# File 'app/models/ui_changed/screenshot.rb', line 46

def check_image_content_type
  errors.add(:image_content_type, "can only be png") unless image_content_type == "png"
end

#check_only_one_typeObject



30
31
32
33
34
35
# File 'app/models/ui_changed/screenshot.rb', line 30

def check_only_one_type
  types = [is_control, is_test, is_compare]
  if types.count(true) != 1
    errors[:base] << 'screenshot can only have one is value set to true'
  end
end

#displayable_image_path_fullObject



223
224
225
# File 'app/models/ui_changed/screenshot.rb', line 223

def displayable_image_path_full
  image_path_full.sub('public','')
end

#displayable_image_path_small_fullObject



227
228
229
# File 'app/models/ui_changed/screenshot.rb', line 227

def displayable_image_path_small_full
  image_path_small_full.sub('public','')
end

#image_file_name_fullObject



239
240
241
242
243
244
# File 'app/models/ui_changed/screenshot.rb', line 239

def image_file_name_full
  if image_file_name == nil || image_content_type == nil
    return "default.png"
  end
  image_file_name + "." + image_content_type
end

#image_file_name_small_fullObject



246
247
248
249
250
251
# File 'app/models/ui_changed/screenshot.rb', line 246

def image_file_name_small_full
  if image_file_name == nil || image_content_type == nil
    return "default.png"
  end
  image_file_name + "_small." + image_content_type
end

#image_path_baseObject



213
214
215
216
217
218
219
220
221
# File 'app/models/ui_changed/screenshot.rb', line 213

def image_path_base
  if is_control
    UiChanged::ConfigHelper.control_path
  elsif is_compare
    UiChanged::ConfigHelper.compare_path
  else
    UiChanged::ConfigHelper.test_path
  end
end

#image_path_fullObject



231
232
233
# File 'app/models/ui_changed/screenshot.rb', line 231

def image_path_full
  image_path_base + image_file_name_full
end

#image_path_small_fullObject



235
236
237
# File 'app/models/ui_changed/screenshot.rb', line 235

def image_path_small_full
  image_path_base + image_file_name_small_full
end

#move_test_image_to_controlObject



208
209
210
211
# File 'app/models/ui_changed/screenshot.rb', line 208

def move_test_image_to_control
  UiChanged::Screenshot.move_test_image_to_control(image_file_name_full)
  UiChanged::Screenshot.move_test_image_to_control(image_file_name_small_full)
end

#remove_imageObject



203
204
205
206
# File 'app/models/ui_changed/screenshot.rb', line 203

def remove_image
  FileUtils.rm(image_path_full)
  FileUtils.rm(image_path_small_full)
end