fixed registration for module apps. now widget methods are set by module initializer and also widget select and also changed the rendering to html instead of file

This commit is contained in:
Harry Bomrah 2014-04-02 20:08:27 +08:00
parent d173ed28b5
commit e5d3d10263
9 changed files with 62 additions and 118 deletions

View File

@ -3,34 +3,32 @@ class PagePartsController < ApplicationController
def new
@part = PagePart.new
@part.sub_parts.build
@modules = []
@part_number = params[:part]
@page_id = params[:page_id]
Dir.glob("#{Rails.root}/app/templates/#{@key}/modules/*").each do |m|
@modules << m.gsub("#{Rails.root}/app/templates/#{@key}/modules/","")
end
@modules = ModuleApp.all
end
def edit
page = Page.find(params[:page_id])
@part = page.page_parts.where(:part_id => params[:id]).first
@part.sub_parts.build
@modules = []
@page_id = params[:page_id]
Dir.glob("#{Rails.root}/app/templates/#{@key}/modules/*").each do |m|
@modules << m.gsub("#{Rails.root}/app/templates/#{@key}/modules/","")
end
@modules = ModuleApp.all
end
def getwidgets
module_name = params[:module]
@select_number = params[:number_of_select]
@widgets = []
Dir.glob("#{Rails.root}/app/templates/#{@key}/modules/#{module_name}/*").each do |w|
@widget_types = []
Dir.glob("#{Rails.root}/app/templates/#{@key}/modules/#{module_name.downcase.pluralize}/*").each do |w|
w = File.basename(w, ".*")
w = File.basename(w, ".*")
@widgets << w.gsub("_","") if w != "index" && w != "show"
@widget_types << w.gsub("_","") if w != "index" && w != "show"
end
module_name = module_name.downcase.singularize
app = ModuleApp.find_by_key(module_name)
@widget_methods = app.get_registration.get_widget_methods
render :layout => false
end
@ -43,7 +41,7 @@ class PagePartsController < ApplicationController
subparts = part.sub_parts
subpart = []
subparts.each do |p|
subpart << {"module" => p.module, "widget" => p.widget}
subpart << {"module" => p.module, "widget" => p.widget_type}
end
data << {"name" => partid, "subparts" => subpart}
end
@ -66,7 +64,7 @@ class PagePartsController < ApplicationController
private
def part_params
params.require(:page_part).permit(:part_id, :page_id, sub_parts_attributes: [:module, :widget, :id])
params.require(:page_part).permit(:part_id, :page_id, sub_parts_attributes: [:module, :widget_type,:widget_method, :id])
end
end

View File

@ -44,7 +44,8 @@ class PagesController < ApplicationController
OrbitHelper.set_params params
OrbitHelper.set_site_locale locale
render render_final_page("#{module_app}/#{params[:target_action]}",page)
# render render_final_page("#{module_app}/#{params[:target_action]}",page)
render :html => render_final_page("#{module_app}/#{params[:target_action]}",page).html_safe
end
end
@ -79,7 +80,7 @@ class PagesController < ApplicationController
partials = []
subparts.each do |subpart|
# partials << render_to_string(:partial => get_widget_path(widget_path))
partials << render_widget_for_frontend(params[:target_controller],"widget",subpart.widget)
partials << render_widget_for_frontend(params[:target_controller],subpart.widget_method,subpart.widget_type)
end
@part_partials["data-pp='#{part.part_id}'"] = partials
end
@ -100,17 +101,17 @@ class PagesController < ApplicationController
pp.inner_html = html_string
end
viewarea = doc.css("*[data-content='true']")
viewarea = viewarea[0]
viewarea = doc.css("*[data-content='true']")[0]
viewarea.inner_html = render_to_string(original_view)
newlayout = "#{page.name}_layout.html.erb"
file_path = File.join(Rails.root, 'app', 'views', 'frontend', newlayout)
# newlayout = "#{page.name}_layout.html.erb"
# file_path = File.join(Rails.root, 'app', 'views', 'frontend', newlayout)
f = File.open(file_path,"w")
f.write(doc.to_html)
f.close
view_to_render = "frontend/#{newlayout}"
view_to_render
# f = File.open(file_path,"w")
# f.write(doc.to_html)
# f.close
# view_to_render = "frontend/#{newlayout}"
# view_to_render
doc.to_html
end
def get_widget_path(widget)

View File

@ -12,7 +12,7 @@ module PagesHelper
end
def render_widget_for_frontend(controller_name, widget_method, widget_file)
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")
file.close
wrap_elements = doc.css("*[data-repeat]")

View File

@ -2,5 +2,13 @@ class ModuleApp
include Mongoid::Document
field :title, type: String
field :key, type: String
field :widget_methods, type: Array
def get_registration
OrbitApp::Module::Registration.find_by_key(key)
end
def self.find_by_key(key)
self.find_by(:key => key)
end
end

View File

@ -3,7 +3,8 @@ class SubPart
include Mongoid::Timestamps
field :module
field :widget
field :widget_type
field :widget_method
belongs_to :page_part
end

View File

@ -1,83 +0,0 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<link rel="shortcut icon" href="/assets/images/favicon.ico">
<title>Orbit Classic</title>
<link href="/assets/orbit_classic.css?body=1" media="screen" rel="stylesheet">
<link href="/assets/bootstrap.css?body=1" media="screen" rel="stylesheet">
<link href="/assets/font-awesome.css?body=1" media="screen" rel="stylesheet">
<link href="/assets/icon-ie7.min.css?body=1" media="screen" rel="stylesheet">
<link href="/assets/icon.css?body=1" media="screen" rel="stylesheet">
<link href="/assets/main.css?body=1" media="screen" rel="stylesheet">
<script src="/assets/lib/jquery-1.11.0.min.js?body=1"></script><script src="/assets/plugin/bootstrap.js?body=1"></script><script src="/assets/plugin/html5shiv.js?body=1"></script><script src="/assets/plugin/jquery.lite.image.resize.js?body=1"></script><script src="/assets/plugin/response.min.js?body=1"></script><script src="/assets/orbit_classic.js?body=1"></script>
</head>
<body>
<div class="navbar navbar-fixed-top navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Project name</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav menu-level-0" data-menu-level="0">
<li>
<a href="/en/news" data-menu-link="true">
News
<b class="caret"></b>
</a>
<ul class="nav navbar-nav dropdown-menu menu-level-1" data-menu-level="1"><li>
<a href="/en/news/focusnews" data-menu-link="true">
Focus News
<b class="caret"></b>
</a>
<ul class="dropdown-menu menu-level-2" data-menu-level="2"><li>
<a href="/en/news/focusnews/admission" data-menu-link="true">
Admissions
</a>
</li></ul>
</li></ul>
</li>
<li>
<a href="/en/faqs" data-menu-link="true">
Faqs
<b class="caret"></b>
</a>
</li>
</ul>
</div>
<!-- /.nav-collapse -->
</div>
<!-- /.container -->
</div>
<div class="container">
<div class="row row-offcanvas row-offcanvas-right">
<div class="col-xs-12 col-sm-9">
<header data-pp="1"></header><main class="row" data-content="true">
<article><h1>Chinese params test</h1>
<section><div>Chinese params test body</div>
</section></article></main><!--/row-->
</div>
<!--/span-->
<div class="col-xs-6 col-sm-3 sidebar-offcanvas" id="sidebar" role="navigation" data-pp="2"></div>
<!--/span-->
</div>
<!--/row-->
<hr>
<footer><p>© Company 2014</p>
</footer>
</div>
</body>
</html>

View File

@ -2,10 +2,10 @@
<%= f.hidden_field :page_id, value: @page_id %>
<%= f.fields_for :sub_parts do |sub| %>
Module :
<%= sub.select(:module, @modules,{:include_blank => true}, "data-number"=> "pp_#{i}", "class" => "module_name") %>
<%= sub.select(:module, @modules.map{|m| [m.title,m.key]},{:include_blank => true}, "data-number"=> "pp_#{i}", "class" => "module_name") %>
<span for="pp_<%= i.to_s %>">
<%= sub.label(:widget,@part.sub_parts[i].widget) rescue nil %>
<%= sub.hidden_field(:widget) if i < @part.sub_parts.count %>
<%= sub.label(:widget_type,@part.sub_parts[i].widget) rescue nil %>
<%= sub.hidden_field(:widget_type) if i < @part.sub_parts.count %>
</span>
<% i = i + 1 %>
<br />

View File

@ -1,2 +1,4 @@
Select Widget
<%= select_tag("page_part[sub_parts_attributes][#{@select_number}][widget]",options_for_select(@widgets),{:include_blank => true}) %>
Select Widget Type
<%= select_tag("page_part[sub_parts_attributes][#{@select_number}][widget_type]",options_for_select(@widget_types),{:include_blank => true}) %>
Select Widget Method
<%= select_tag("page_part[sub_parts_attributes][#{@select_number}][widget_method]",options_for_select(@widget_methods),{:include_blank => true}) %>

View File

@ -29,13 +29,14 @@ module OrbitApp
end
class RegisteredModule
attr_reader :name,:key,:module_label, :widget_methods
attr_reader :name,:key,:module_label,:widget_methods
def initialize(name,&block)
@name = name
@key = @name.underscore.singularize
# @module_label = 'rulingcom.errors.init.module_app_noname'
@module_label = @name
@widget_methods = []
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
setup_module_app
end
@ -45,10 +46,26 @@ module OrbitApp
def setup_module_app
module_app = get_module_app
module_app = ModuleApp.new(:key=>@key,:title=>name, :widget_methods => @widget_methods) if module_app.nil?
module_app = ModuleApp.new(:key=>@key,:title=>name) if module_app.nil?
module_app.save(:validate=>false)
end
%w{data_count module_label category base_url version organization author intro update_info}.each do |field|
define_method(field){|var| instance_variable_set( "@" + field, var)}
end
def module_label(name = @name)
@module_label = name
end
def widget_methods(widgets)
@widget_methods = widgets
end
def get_widget_methods
@widget_methods
end
end
end
end