OSDN Git Service

t#29061/t#29089/t#29076:add mylist. modify panel picture
[pettanr/pettanr.git] / lib / validators / sync_vh_validator.rb
diff --git a/lib/validators/sync_vh_validator.rb b/lib/validators/sync_vh_validator.rb
new file mode 100644 (file)
index 0000000..928d957
--- /dev/null
@@ -0,0 +1,15 @@
+class SyncVhValidator < ActiveModel::EachValidator\r
+  def validate_each(record, attribute, value)\r
+    if record.picture.flag_sync_vh < 0\r
+      record.errors[attribute] << (options[:message] || "sync_vh") unless sync?(record)\r
+    end\r
+  end\r
+  \r
+  def sync?(record)\r
+    r = record.width * 100 / record.height\r
+    r2 = record.picture.width * 100 / record.picture.height\r
+    (r >= r2 - 1) and (r <= r2 + 1)\r
+  end\r
+  \r
+end\r
+\r