Class: C80NewsTz::Fact

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/c80_news_tz/fact.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.similar_issue_pubs(pub, count) ⇒ Object

выдать нужное количество публикаций из того же номера



143
144
145
146
147
148
149
# File 'app/models/c80_news_tz/fact.rb', line 143

def self.similar_issue_pubs(pub, count)
  r = self.joins(:issues)
          .where(:c80_news_tz_issues => {:id => pub.issue_id})
          .where.not(:c80_news_tz_facts => {:id => pub.id})
          .limit(count)
  r
end

.similar_rubric_pubs(pub, count) ⇒ Object

выдать нужное количество публикаций из той же рубрики



134
135
136
137
138
139
140
# File 'app/models/c80_news_tz/fact.rb', line 134

def self.similar_rubric_pubs(pub, count)
  r = self.joins(:rubrics)
      .where(:c80_news_tz_rubrics => {:id => pub.rubric_id})
      .where.not(:c80_news_tz_facts => {:id => pub.id})
      .limit(count)
  r
end

.where_locations(arr_locs_ids) ⇒ Object

выдать массив хэшей: список фактов, находящихся в указанных позициях, отсортированный по location_id



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/c80_news_tz/fact.rb', line 108

def self.where_locations(arr_locs_ids)
  # f = C80NewsTz::Fact.joins(:locations).where(:c80_news_tz_locations => {:id => [1,2]})

  s = "
    SELECT
      `c80_news_tz_facts`.*,
      `c80_news_tz_locations`.`id` AS `location_id`
    FROM `c80_news_tz_facts`
      INNER JOIN `c80_news_tz_facts_locations` ON `c80_news_tz_facts_locations`.`fact_id` = `c80_news_tz_facts`.`id`
      INNER JOIN `c80_news_tz_locations` ON `c80_news_tz_locations`.`id` = `c80_news_tz_facts_locations`.`location_id`
    WHERE `c80_news_tz_locations`.`id` IN (#{arr_locs_ids.join(",")})
    ORDER BY `location_id`;
  "

  # result = self.connection.select_all(s)
  result = self.find_by_sql(s)

  result
end

.where_rubric(rubric_slug) ⇒ Object

выдать факты, принадлежащие указанной рубрике



129
130
131
# File 'app/models/c80_news_tz/fact.rb', line 129

def self.where_rubric(rubric_slug)
  self.joins(:rubrics).where(:c80_news_tz_rubrics => {:slug => rubric_slug})
end

Instance Method Details

#company_logoObject

выдать логотип первой попавшейся компании если чего-то нету - выдаётся nil



97
98
99
100
101
102
103
104
105
# File 'app/models/c80_news_tz/fact.rb', line 97

def 
  result = nil
  if companies.count > 0
    if companies.first..present?
      result = companies.first.
    end
  end
  result
end

#issue_idObject

выдать id первого попавшегося номера если номера у публикации нету - выдать невероятное число 999999



77
78
79
80
81
82
83
# File 'app/models/c80_news_tz/fact.rb', line 77

def issue_id
  result = 999999
  if issues.count > 0
    result = issues.first.id
  end
  result
end

#issue_titleObject

выдать название номера, которому принадлежит новость если чего-то не хватает - выдаётся пустая строка



87
88
89
90
91
92
93
# File 'app/models/c80_news_tz/fact.rb', line 87

def issue_title
  result = ""
  if issues.count > 0
    result = issues.first.number
  end
  result
end

#normalize_friendly_id(input) ⇒ Object



24
25
26
# File 'app/models/c80_news_tz/fact.rb', line 24

def normalize_friendly_id(input)
  input.to_s.to_slug.normalize(transliterations: :russian).to_s
end

#photo_previewObject

выдать картинку, которая пойдёт в блок преьвю



46
47
48
49
50
51
52
53
# File 'app/models/c80_news_tz/fact.rb', line 46

def photo_preview
  result = nil
  if fphotos.count > 0
    result = fphotos.first.image
  else
  end
  result
end

#rubric_idObject

выдать id первой попавшейся рубрики если рубрики у публикации нету - выдать невероятное число 999999



67
68
69
70
71
72
73
# File 'app/models/c80_news_tz/fact.rb', line 67

def rubric_id
  result = 999999
  if rubrics.count > 0
    result = rubrics.first.id
  end
  result
end

#rubric_titleObject

выдать строку: title первой попавшейся рубрики если таковой нету - выдаётся пустая строка



57
58
59
60
61
62
63
# File 'app/models/c80_news_tz/fact.rb', line 57

def rubric_title
  result = ""
  if rubrics.count > 0
    result = rubrics.first.title
  end
  result
end

#short_meta_descriptionObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/c80_news_tz/fact.rb', line 32

def short_meta_description

  if description.present? && description.length > 200
    result = description
  elsif full.present? && full.length > 0
    result = strip_tags(full[0..200]+"...")
  else
    result = nil
  end
  result

end

#slug_candidatesObject



28
29
30
# File 'app/models/c80_news_tz/fact.rb', line 28

def slug_candidates
  [:title] + Array.new(6) {|index| [:title, index+2]}
end