Showing
2 changed files
with
53 additions
and
54 deletions
| ... | ... | @@ -3,6 +3,11 @@ module Kanjai |
| 3 | 3 | |
| 4 | 4 | attr_reader :domain, :lang, :url |
| 5 | 5 | |
| 6 | + PAGE_ID = 0 | |
| 7 | + PARENT_PAGE_ID=1 | |
| 8 | + PAGE_URL=2 | |
| 9 | + PAGE_DATA_ID=3 | |
| 10 | + | |
| 6 | 11 | def initialize(domain:, lang:, url:) |
| 7 | 12 | @domain = domain |
| 8 | 13 | @lang = lang |
| ... | ... | @@ -14,65 +19,35 @@ module Kanjai |
| 14 | 19 | root_page = nil |
| 15 | 20 | page_datum = nil |
| 16 | 21 | |
| 17 | - params = {} | |
| 18 | - | |
| 19 | - loop do | |
| 20 | - if mas.count == 0 && page_datum | |
| 21 | - page_url = page_datum.page.menu_url('http', lang) | |
| 22 | - regust_url = "/#{lang}#{url}" | |
| 23 | - | |
| 24 | - page_url_mas = page_url.split('/') | |
| 25 | - regust_url_mas = regust_url.split('/') | |
| 26 | - page_url_mas.each_with_index do |segment, index| | |
| 27 | - if segment == ':param' | |
| 28 | - page_url_mas[index] = regust_url_mas[index] | |
| 29 | - params[:param] = regust_url_mas[index] | |
| 30 | - end | |
| 31 | - end | |
| 32 | 22 | |
| 33 | - if page_url_mas.join('/') != regust_url_mas.join('/') | |
| 34 | - page_datum = nil | |
| 35 | - end | |
| 36 | - end | |
| 37 | - | |
| 38 | - break if mas.count == 0 | |
| 39 | - | |
| 40 | - check_length = mas.count | |
| 23 | + params = {} | |
| 41 | 24 | |
| 42 | - loop do | |
| 43 | - break if check_length <= 0 | |
| 25 | + page_urls = prepare_data | |
| 26 | + record = page_urls.select{|item| item[:url] == url }.first | |
| 27 | + if record | |
| 28 | + page_datum = Kanjai::PageDatum.find(record[:data_id]) | |
| 29 | + else | |
| 30 | + collection = page_urls.select{|item| item[:url].to_s.include?(':param') } | |
| 31 | + collection.each do |item| | |
| 32 | + if page_datum.nil? | |
| 33 | + mas_url = url.split('/') | |
| 34 | + mas_item_url = item[:url].split('/') | |
| 35 | + | |
| 36 | + if mas_url.count == mas_item_url.count | |
| 37 | + mas_item_url.each_with_index do |segment, index| | |
| 38 | + if segment == ':param' | |
| 39 | + mas_item_url[index] = mas_url[index] | |
| 40 | + params[:param] = mas_url[index] | |
| 41 | + end | |
| 42 | + end | |
| 44 | 43 | |
| 45 | - work_url = mas[0...check_length].join('/') | |
| 46 | - work_url = '/' if work_url.to_s.empty? | |
| 47 | - sheck_scope = scope | |
| 48 | - if root_page | |
| 49 | - page_ids = [].tap do |n| | |
| 50 | - root_page.walk_tree do |page, level| | |
| 51 | - n << page.id | |
| 44 | + if mas_url == mas_item_url | |
| 45 | + page_datum = Kanjai::PageDatum.find(item[:data_id]) | |
| 52 | 46 | end |
| 53 | - end | |
| 54 | - sheck_scope = sheck_scope.where(kanjai_pages: {id: page_ids}) | |
| 55 | - end | |
| 56 | 47 | |
| 57 | - page_datum = sheck_scope.find_by(url: work_url) | |
| 58 | - page_datum = sheck_scope.find_by(url: "/#{work_url}") if page_datum.nil? | |
| 59 | - page_datum = sheck_scope.find_by(url: "#{work_url}/") if page_datum.nil? | |
| 60 | - page_datum = sheck_scope.find_by(url: "/#{work_url}/") if page_datum.nil? | |
| 61 | - page_datum = sheck_scope.find_by(url: ":param") if page_datum.nil? && check_length == 1 | |
| 62 | - | |
| 63 | - if page_datum | |
| 64 | - root_page = page_datum.page | |
| 65 | - mas = mas.last(mas.count - check_length) | |
| 66 | - break | |
| 67 | - else | |
| 68 | - check_length -= 1 | |
| 48 | + end | |
| 69 | 49 | end |
| 70 | 50 | end |
| 71 | - break if page_datum.nil? | |
| 72 | - end | |
| 73 | - | |
| 74 | - if page_datum.nil? && url == '/' | |
| 75 | - page_datum = scope.find_by(url: url) | |
| 76 | 51 | end |
| 77 | 52 | |
| 78 | 53 | {page: page_datum, params: params} |
| ... | ... | @@ -81,7 +56,31 @@ module Kanjai |
| 81 | 56 | private |
| 82 | 57 | |
| 83 | 58 | def scope |
| 84 | - @scope ||= PageDatum.joins(page: :domain).where(lang: lang, kanjai_domains: {id: domain.id}) | |
| 59 | + @scope ||= domain.pages.joins("left join kanjai_page_data on kanjai_pages.id = kanjai_page_data.page_id and kanjai_page_data.lang='#{lang}'").pluck(:id, :parent_id, 'kanjai_page_data.url', "kanjai_page_data.id") | |
| 60 | + end | |
| 61 | + | |
| 62 | + def prepare_data | |
| 63 | + [].tap do |n| | |
| 64 | + scope.each do |item| | |
| 65 | + url = ((item[PAGE_URL].to_s.first == '/' ? [] : generate_url(item[PARENT_PAGE_ID])) + [item[PAGE_URL]]).flatten.reject { |e| e.nil? || e&.empty? || e.to_s == '/' }.join('/') | |
| 66 | + url = '/' + url if url.to_s.present? && url.first != '/' | |
| 67 | + n << { | |
| 68 | + id: item[PAGE_ID], | |
| 69 | + data_id: item[PAGE_DATA_ID], | |
| 70 | + url: url | |
| 71 | + } | |
| 72 | + end | |
| 73 | + end | |
| 74 | + end | |
| 75 | + | |
| 76 | + def generate_url(parent_id) | |
| 77 | + url = [] | |
| 78 | + if parent_id | |
| 79 | + parent_item = scope.select{|item| item[PAGE_ID] == parent_id }.first | |
| 80 | + url = [parent_item[PAGE_URL]] + url | |
| 81 | + url = (parent_item[PAGE_URL].to_s.first == '/' ? [] : generate_url(parent_item[PARENT_PAGE_ID])) + url | |
| 82 | + end | |
| 83 | + return url | |
| 85 | 84 | end |
| 86 | 85 | |
| 87 | 86 | end | ... | ... |