Class: Redde::SeoValidator

Inherits:
Object
  • Object
show all
Defined in:
app/validators/redde/seo_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seo) ⇒ SeoValidator

Returns a new instance of SeoValidator.



6
7
8
# File 'app/validators/redde/seo_validator.rb', line 6

def initialize(seo)
  @seo = seo
end

Instance Attribute Details

#seoObject

Returns the value of attribute seo.



4
5
6
# File 'app/validators/redde/seo_validator.rb', line 4

def seo
  @seo
end

Instance Method Details

#all_fields_present?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/validators/redde/seo_validator.rb', line 50

def all_fields_present?
  url_present? && object_present?
end

#any_field_present?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/validators/redde/seo_validator.rb', line 46

def any_field_present?
  url_present? || object_present?
end

#check_object_uniquenessObject



34
35
36
# File 'app/validators/redde/seo_validator.rb', line 34

def check_object_uniqueness
  @seo.errors[:base] << 'Нарушение уникальности привязки. Два разных сео не могут быть привязаны к одному объекту' if object_uniq?
end

#check_url_uniquenessObject



26
27
28
# File 'app/validators/redde/seo_validator.rb', line 26

def check_url_uniqueness
  @seo.errors[:base] << 'Нарушение уникальности привязки. Два разных сео не могут быть привязаны к одному url' if url_uniq?
end

#edit_or_create_countObject



22
23
24
# File 'app/validators/redde/seo_validator.rb', line 22

def edit_or_create_count
  seo.persisted? ? 1 : 0
end

#empty_fieldsObject



42
43
44
# File 'app/validators/redde/seo_validator.rb', line 42

def empty_fields
  @seo.errors[:base] << 'Отсутствует привязка. Сео дожно быть привязано к url или объекту' unless any_field_present?
end

#object_present?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/validators/redde/seo_validator.rb', line 58

def object_present?
  seo.seoable_type.present?
end

#object_uniq?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/validators/redde/seo_validator.rb', line 38

def object_uniq?
  object_present? && Redde::Seo.where(seoable_type: @seo.seoable_type, seoable_id: @seo.seoable_id).count > edit_or_create_count
end

#url_present?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/validators/redde/seo_validator.rb', line 54

def url_present?
  seo.url.present?
end

#url_uniq?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/validators/redde/seo_validator.rb', line 30

def url_uniq?
  url_present? && Redde::Seo.where(url: seo.url).count > edit_or_create_count
end

#valid?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/validators/redde/seo_validator.rb', line 18

def valid?
  !seo.errors.any?
end

#validateObject



10
11
12
13
14
15
16
# File 'app/validators/redde/seo_validator.rb', line 10

def validate
  empty_fields
  if valid?
    check_url_uniqueness
    check_object_uniqueness
  end
end