completed home page rendering...

This commit is contained in:
Harry Bomrah 2014-04-07 15:57:06 +08:00
parent 3ca008e041
commit 5a5107a152
5 changed files with 42 additions and 23 deletions

View File

@ -16,9 +16,18 @@ class PagesController < ApplicationController
render get_view render get_view
end end
def home
@manifest = @key
@dataApi = nil
page = Page.where(:number => 0,:page_id => "").first
OrbitHelper.set_params params
OrbitHelper.set_site_locale locale
render :html => render_final_page("home",page).html_safe
end
def show def show
path = request.original_fullpath.split('/') path = request.original_fullpath.split('/')
if path.size == 2 if path.size <= 2
redirect_to root_path redirect_to root_path
else else
if path.last.include? '-' if path.last.include? '-'
@ -79,15 +88,18 @@ class PagesController < ApplicationController
subparts = part.sub_parts subparts = part.sub_parts
partials = [] partials = []
subparts.each do |subpart| subparts.each do |subpart|
# partials << render_to_string(:partial => get_widget_path(widget_path)) partials << render_widget_for_frontend(subpart.module,subpart.widget_method,subpart.widget_type)
partials << render_widget_for_frontend(params[:target_controller],subpart.widget_method,subpart.widget_type)
end end
@part_partials["data-pp='#{part.part_id}'"] = partials @part_partials["data-pp='#{part.part_id}'"] = partials
end end
@file = nil @file = nil
@layout_html = nil @layout_html = nil
@file = File.join('../templates', "#{@key}", '/home/page.html.erb') if original_view == "home"
@file = File.join('../templates', "#{@key}", '/home/index.html.erb')
else
@file = File.join('../templates', "#{@key}", '/home/page.html.erb')
end
@layout_html = render_to_string(@file) @layout_html = render_to_string(@file)
doc = Nokogiri::HTML(@layout_html, nil, "UTF-8") doc = Nokogiri::HTML(@layout_html, nil, "UTF-8")
@ -101,8 +113,10 @@ class PagesController < ApplicationController
pp.inner_html = html_string pp.inner_html = html_string
end end
viewarea = doc.css("*[data-content='true']")[0] if original_view != "home"
viewarea.inner_html = render_to_string(original_view) viewarea = doc.css("*[data-content='true']")[0]
viewarea.inner_html = render_to_string(original_view)
end
# newlayout = "#{page.name}_layout.html.erb" # newlayout = "#{page.name}_layout.html.erb"
# file_path = File.join(Rails.root, 'app', 'views', 'frontend', newlayout) # file_path = File.join(Rails.root, 'app', 'views', 'frontend', newlayout)
@ -119,10 +133,9 @@ class PagesController < ApplicationController
end end
def get_view def get_view
page = Page.find(params[:id]) page = Page.find(params[:id])
if page.page_id == "" && page.number == 0
if page.page_id == "home" @view = File.join(Rails.root, 'app', 'templates', "#{@key}", 'home/index.html.erb')
@view = File.join(Rails.root, 'app', 'templates', "#{@key}", '/home/index.html.erb')
else else
module_name = page.module.downcase.pluralize module_name = page.module.downcase.pluralize
@view = File.join(Rails.root, 'app', 'templates', "#{@key}", "modules/#{module_name}/index.html.erb") @view = File.join(Rails.root, 'app', 'templates', "#{@key}", "modules/#{module_name}/index.html.erb")
@ -145,7 +158,7 @@ class PagesController < ApplicationController
def get_layout def get_layout
if request[:action] == "edit_view" if request[:action] == "edit_view"
page = Page.find(params[:id]) page = Page.find(params[:id])
if page.page_id == "home" if page.page_id == "" && page.number == 0
false false
else else
File.join("../../templates", "#{@key}", '/home/page.html.erb') File.join("../../templates", "#{@key}", '/home/page.html.erb')

View File

@ -8,7 +8,12 @@ module OrbitHelper
end end
def self.url_to_show(slug) def self.url_to_show(slug)
"/#{@site_locale}#{@params[:url]}/#{slug}" if @params[:url]
"/#{@site_locale}#{@params[:url]}/#{slug}"
else
page = Page.where(:module => self.current_widget_module).first
"/#{@site_locale}#{page.url}/#{slug}"
end
end end
def self.set_site_locale(locale) def self.set_site_locale(locale)
@ -18,12 +23,12 @@ module OrbitHelper
def self.get_site_locale def self.get_site_locale
@site_locale @site_locale
end end
def self.to_param(title) def self.current_widget_module
if I18n.locale.to_s == "en" @controller_name
title.parameterize end
else
title def self.set_current_widget_module(name)
end @controller_name = name
end end
end end

View File

@ -12,11 +12,13 @@ module PagesHelper
end end
def render_widget_for_frontend(controller_name, widget_method, widget_file) def render_widget_for_frontend(controller_name, widget_method, widget_file)
controller_name = controller_name.downcase.pluralize
file = File.open(File.join(Rails.root, 'app', 'templates', "#{Template::KEY}", 'modules', "#{controller_name}", "_#{widget_file}.html.erb")) file = File.open(File.join(Rails.root, 'app', 'templates', "#{Template::KEY}", 'modules', "#{controller_name}", "_#{widget_file}.html.erb"))
doc = Nokogiri::HTML(file, nil, "UTF-8") doc = Nokogiri::HTML(file, nil, "UTF-8")
file.close file.close
wrap_elements = doc.css("*[data-repeat]") wrap_elements = doc.css("*[data-repeat]")
controller = "#{controller_name.capitalize}Controller".classify.constantize.new controller = "#{controller_name.capitalize}Controller".classify.constantize.new
OrbitHelper.set_current_widget_module controller_name.downcase.singularize
data = controller.send("#{widget_method}") data = controller.send("#{widget_method}")
keys = data.keys keys = data.keys
wrap_elements.each do |wrap_element| wrap_elements.each do |wrap_element|

View File

@ -10,5 +10,4 @@
</div> </div>
</div> </div>
</div> </div>
<p class="text-center"><a data-href="more" class="btn btn-primary" role="button" href="{{link_to_index}}">More</a></p> <p class="text-center"><a data-href="more" class="btn btn-primary" role="button" href="{{link_to_index}}">More</a></p>
<p class="text-center">{{name}}</p>

View File

@ -20,7 +20,7 @@ OrbitStore::Application.routes.draw do
# See how all your routes lay out with "rake routes". # See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root" # You can have the root of your site routed with "root"
root 'home#index' root 'pages#home'
scope "(:locale)", locale: /en|zh_tw/ do scope "(:locale)", locale: /en|zh_tw/ do
get ':page(/:page)(/:page)(/:page)', to: 'pages#show', constraints: KeywordConstraint.new get ':page(/:page)(/:page)(/:page)', to: 'pages#show', constraints: KeywordConstraint.new
resources :pages resources :pages