Class: Air18n::PhraseScreenshot
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Air18n::PhraseScreenshot
- Defined in:
- lib/air18n/phrase_screenshot.rb
Class Method Summary collapse
-
.all_phrase_urls ⇒ Object
Returns 2D array of phrase_keys => array of routes_contexts for which we have screenshots.
-
.associate_phrase_with_screenshot(phrase_key, controller_name, action_name, screenshot_url) ⇒ Object
Associates a phrase with a controller, action pair and a screenshot.
- .deserialize_page_id(page_id) ⇒ Object
-
.get_screenshots_ordered_by_relevance(screenshots_for_a_phrase, controller_name, action_name) ⇒ Object
Returns the screenshots associated with phrase, with the screenshots for the specified controller and action first in the list.
- .get_screenshots_ordered_by_relevance_for_many_phrases(phrases, controller_name, action_name) ⇒ Object
- .make_routes_context(controller, action) ⇒ Object
- .serialize_page_id(controller, action) ⇒ Object
Instance Method Summary collapse
Class Method Details
.all_phrase_urls ⇒ Object
Returns 2D array of phrase_keys => array of routes_contexts for which we have screenshots
13 14 15 16 17 18 19 20 |
# File 'lib/air18n/phrase_screenshot.rb', line 13 def self.all_phrase_urls Hash.new { |h, k| h[k] = [] }.tap do |all_phrase_urls| # Uses raw SQL for speed. connection.select_all("SELECT `phrase_key`, `action`, `controller` FROM phrase_screenshots where screenshot_url != ''").each do |record| all_phrase_urls[record['phrase_key']] << make_routes_context(record['controller'], record['action']) end end end |
.associate_phrase_with_screenshot(phrase_key, controller_name, action_name, screenshot_url) ⇒ Object
Associates a phrase with a controller, action pair and a screenshot. Set screenshot_url to the empty string if you don’t have a screenshot. Returns the PhraseScreenshot.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/air18n/phrase_screenshot.rb', line 82 def self.associate_phrase_with_screenshot( phrase_key, controller_name, action_name, screenshot_url) ps = PhraseScreenshot.find_or_create_by_controller_and_action_and_phrase_key( controller_name, action_name, phrase_key) do |new_phrase_screenshot| new_phrase_screenshot.screenshot_url = screenshot_url end # If the PhraseScreenshot already existed, update the screenshot URL. if ps.screenshot_url.blank? && ps.screenshot_url != screenshot_url ps.screenshot_url = screenshot_url ps.save # Make sure that this new screenshot URL has a row in the screenshots # table. ps.create_row_in_screenshots end ps end |
.deserialize_page_id(page_id) ⇒ Object
74 75 76 77 |
# File 'lib/air18n/phrase_screenshot.rb', line 74 def self.deserialize_page_id(page_id) controller, action = page_id.split("#") Struct.new(:controller, :action).new(controller, action) end |
.get_screenshots_ordered_by_relevance(screenshots_for_a_phrase, controller_name, action_name) ⇒ Object
Returns the screenshots associated with phrase, with the screenshots for the specified controller and action first in the list. If controller_name or action_name are nil, result is unordered.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/air18n/phrase_screenshot.rb', line 57 def self.get_screenshots_ordered_by_relevance(screenshots_for_a_phrase, controller_name, action_name) if controller_name || action_name screenshots_for_a_phrase.sort_by do |x| priority = 0 priority -= 1 if x.action == action_name priority -= 2 if x.controller == controller_name priority end else screenshots_for_a_phrase end end |
.get_screenshots_ordered_by_relevance_for_many_phrases(phrases, controller_name, action_name) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/air18n/phrase_screenshot.rb', line 44 def self.get_screenshots_ordered_by_relevance_for_many_phrases(phrases, controller_name, action_name) screenshots = PhraseScreenshot.includes(:screenshot).where(:phrase_key => phrases.collect(&:key)) screenshots_for_each_phrase = Hash.new { |h, k| h[k] = [] } screenshots.all.each { |screenshot| screenshots_for_each_phrase[screenshot.phrase_key] << screenshot } screenshots_for_each_phrase.inject(Hash.new([])) do |carry, (key, screenshots_for_phrase)| carry[key] = self.get_screenshots_ordered_by_relevance(screenshots_for_phrase, controller_name, action_name) carry end end |
.make_routes_context(controller, action) ⇒ Object
31 32 33 |
# File 'lib/air18n/phrase_screenshot.rb', line 31 def self.make_routes_context(controller, action) "#{controller}-#{action}" end |
.serialize_page_id(controller, action) ⇒ Object
70 71 72 |
# File 'lib/air18n/phrase_screenshot.rb', line 70 def self.serialize_page_id(controller, action) "#{controller}##{action}" end |
Instance Method Details
#create_row_in_screenshots ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/air18n/phrase_screenshot.rb', line 35 def create_row_in_screenshots if !Screenshot.where(:screenshot_url => self.screenshot_url).exists? Screenshot.create do |s| s.screenshot_url = self.screenshot_url s.status = 0 end end end |
#has_position? ⇒ Boolean
22 23 24 25 |
# File 'lib/air18n/phrase_screenshot.rb', line 22 def has_position? return width.present? && height.present? && x.present? && y.present? && width > 0 && height > 0 && x > 0 && y > 0 end |
#routes_context ⇒ Object
27 28 29 |
# File 'lib/air18n/phrase_screenshot.rb', line 27 def routes_context self.class.make_routes_context(self.controller, self.action) end |