lib/amrita2/rails_bridge.rb in amrita2-1.9.6 vs lib/amrita2/rails_bridge.rb in amrita2-2.0.0
- old
+ new
@@ -1,40 +1,186 @@
require 'amrita2/template'
-module Amrita2View
+require 'action_view'
+
+module ActionView # :nodoc: all
+ class Base #:nodoc:
+ include Amrita2::Runtime
+ end
+end
+
+module ActiveRecord # :nodoc: all
class Base
+ include Amrita2::DictionaryData
+ end
+
+ class ConnectionAdapters::Column
+ include Amrita2::DictionaryData
+ end
+end
+
+module ActionController # :nodoc: all
+ class Pagination::Paginator
+ include Amrita2::DictionaryData
+ end
+end
+
+
+module Amrita2View # :nodoc: all
+ class Base
+ include Amrita2
+ include Amrita2::Filters
+ include Amrita2::Runtime
+ include Amrita2::Util
+
+ CompileTimeBinding = binding
@@compiled_amrita2_templates = {}
+ @@text_domain = nil
+ cattr_accessor :text_domain
+
def initialize( action_view )
@action_view = action_view
- @action_view.extend(Amrita2::Runtime)
end
- def render( template, local_assigns={} )
- action_name = @action_view.controller.action_name
- tmpl_method = action_name + "_setup_template"
- tmpl = @@compiled_amrita2_templates[template] ||=
- if @action_view.respond_to?(tmpl_method)
- @action_view.send(tmpl_method, template)
- else
- t = Amrita2::TemplateText.new(template)
- b = @action_view.instance_eval { binding }
- t.use_erb(b)
- t
- end
+ def render(template, local_assigns={})
+ Thread::current[:amrita_rails_view] = @action_view
+ if template.kind_of?(String)
+ render_amrita(template, local_assigns)
+ else
+ @action_view.render(template, local_assigns)
+ end
+ end
- po_method = action_name + "_po"
- po = nil
- if @action_view.respond_to?(po_method)
- po = @action_view.send(po_method)
+ def setup_template(template)
+ setup_template_default(template)
+ end
+
+ def setup_template_default(template)
+ if Amrita2::const_defined?(:GetTextBridge)
+ t = Amrita2::Template.new(template) do |e, src, filters|
+ filters << Amrita2::Filters::GetTextFilter.new
+ end
+ t.text_domain = text_domain
+ bindtextdomain(t.text_domain)
+ #t.set_trace(STDOUT)
+ t.compiletime_binding = CompileTimeBinding
+ t
else
- po = @action_view.controller
- end
+ t = Amrita2::Template.new(template)
+ t.compiletime_binding = CompileTimeBinding
+ t
+ end
+ end
- tmpl.expand(out="", po)
- out
- rescue
- $!.to_s + $@.join("<br>")
+ def render_amrita(template, local_assigns)
+ @@compiled_amrita2_templates[template] ||= setup_template(template)
+ tmpl = @@compiled_amrita2_templates[template]
+ b = setup_binding_of_view(local_assigns)
+ tmpl.render_with(b)
end
+
+ def setup_binding_of_view(local_assigns)
+ @action_view.instance_eval do
+ evaluate_assigns
+ b = binding
+ local_assigns.each do |k, v|
+ amrita_set_context_value(v)
+ eval "#{k}=amrita_get_context_value",b
+ end
+ b
+ end
+ end
end
+ module Helper
+ include ActionView::Helpers::UrlHelper
+
+ def view
+ @view ||= Thread::current[:amrita_rails_view]
+ end
+
+ def eval_in_view(&block)
+ view.instance_eval &block
+ end
+
+ def eval_in_view_without_escape(&block)
+ Amrita2::SanitizedString[eval_in_view(&block)]
+ end
+ end
end
+
+=begin
+
+ class WithObject < Amrita2::Macro::Base
+ TemplateText = <<-'END'
+ <<%<
+ <% Thread::current[:amrita2_form_object] = $_[:name] || $_[:object] %>
+ <<_ :| Attr[:target_src=>:object]<
+ <<:contents>>
+ END
+
+ Option = {
+ :tag => "a:with_object",
+ :use_contents => :contents,
+ }
+
+ end
+
+ class Input < Amrita2::Macro::Base
+ TemplateText = <<-'END'
+ <<%<
+ <%
+ object = $_.delete(:object) || Thread::current[:amrita2_form_object]
+ input_id = $_.delete(:id)
+ other = $_.collect do |k, v|
+ "#{k}='#{v}'"
+ end
+ %>
+ <input id="<%= object %>_<%= input_id %>" name="<%= object %>[<%= input_id%>]" <%= other %> target_filter="Attr[:value=><%= input_id.intern.inspect %>]"/>
+ END
+
+ Option = {
+ :tag => "a:input"
+ }
+ end
+
+ class TextField < Amrita2::Macro::Base
+ TemplateText = <<-'END'
+ <<%<
+ <%
+ object = $_.delete(:object) || Thread::current[:amrita2_form_object]
+ input_id = $_.delete(:id)
+ $_[:size] ||= 30
+ other = $_.collect do |k, v|
+ "#{k}='#{v}'"
+ end
+ %>
+ <input type="text" id="<%= object %>_<%= input_id %>" name="<%= object %>[<%= input_id%>]" <%= other %> target_filter="Attr[:value=><%= input_id.intern.inspect %>]"/>
+ END
+
+ Option = {
+ :tag => "a:text_field",
+ }
+ end
+
+ class TextArea < Amrita2::Macro::Base
+ TemplateText = <<-'END'
+ <<%<
+ <%
+ object = $_.delete(:object) || Thread::current[:amrita2_form_object]
+ input_id = $_.delete(:id)
+ $_[:cols] ||= 40
+ $_[:rows] ||= 20
+ other = $_.collect do |k, v|
+ "#{k}='#{v}'"
+ end
+ %>
+ <textarea id="<%= object %>_<%= input_id %>" name="<%= object %>[<%= input_id%>]" <%= other %> target_filter="Attr[:body=><%= input_id.intern.inspect %>]"/>
+ END
+
+ Option = {
+ :tag => "a:textarea",
+ }
+ end
+=end
+