Module: ActionView::Rendering

Defined in:
lib/active_scaffold/extensions/action_view_rendering.rb

Overview

wrap the action rendering for ActiveScaffold views

Instance Method Summary collapse

Instance Method Details

#partial_pieces(partial_path) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/active_scaffold/extensions/action_view_rendering.rb', line 102

def partial_pieces(partial_path)
  if partial_path.include?('/')
    return File.dirname(partial_path), File.basename(partial_path)
  else
    return controller.class.controller_path, partial_path
  end
end

#render_with_active_scaffold(*args, &block) ⇒ Object

Adds two rendering options.

render :super

This syntax skips all template overrides and goes directly to the provided ActiveScaffold templates. Useful if you want to wrap an existing template. Just call super!

render :active_scaffold => #ActionView::Rendering.controllercontroller.to_s, options = {}+

Lets you embed an ActiveScaffold by referencing the controller where it’s configured.

You may specify options for the embedded scaffold. These constraints have three effects:

* the scaffold's only displays records matching the constraint
* all new records created will be assigned the constrained values
* constrained columns will be hidden (they're pretty boring at this point)

You may also specify options for the embedded scaffold. These only do 1/3 of what constraints do (they only limit search results). Any format accepted by ActiveRecord::Base.find is valid.

Defining options lets you completely customize the list title for the embedded scaffold.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_scaffold/extensions/action_view_rendering.rb', line 39

def render_with_active_scaffold(*args, &block)
  if args.first == :super
    last_view = @view_stack.last
    options = args[1] || {}
    options[:locals] ||= {}
    options[:locals].reverse_merge!(last_view[:locals] || {})
    if last_view[:templates].nil?
      last_view[:templates] = lookup_context.find_all_templates(last_view[:view], controller_path, !last_view[:is_template])
      last_view[:templates].shift
    end
    options[:template] = last_view[:templates].shift
    @view_stack << last_view
    result = render_without_active_scaffold options
    @view_stack.pop
    result
  elsif args.first.is_a?(Hash) and args.first[:active_scaffold]
    require 'digest/md5'
    options = args.first

    remote_controller = options[:active_scaffold]
    constraints = options[:constraints]
    conditions = options[:conditions]
    eid = Digest::MD5.hexdigest(params[:controller] + remote_controller.to_s + constraints.to_s + conditions.to_s)
    session["as:#{eid}"] = {:constraints => constraints, :conditions => conditions, :list => {:label => args.first[:label]}}
    options[:params] ||= {}
    options[:params].merge! :eid => eid, :embedded => true
    
    id = "as_#{eid}-content"
    url_options = {:controller => remote_controller.to_s, :action => 'index'}.merge(options[:params])
    
    if controller.respond_to?(:render_component_into_view)
      controller.send(:render_component_into_view, url_options)
    else
      (:div, {:id => id}) do
        url = url_for(url_options)
        link_to(remote_controller.to_s, url, {:remote => true, :id => id}) <<
          if ActiveScaffold.js_framework == :prototype
          javascript_tag("new Ajax.Updater('#{id}', '#{url}', {method: 'get', evalScripts: true});")
        elsif ActiveScaffold.js_framework == :jquery
          javascript_tag("$('##{id}').load('#{url}');")
        end
      end
    end
    
  else
    options = args.first
    if options.is_a?(Hash)
      current_view = {:view => options[:partial], :is_template => false} if options[:partial]
      current_view = {:view => options[:template], :is_template => !!options[:template]} if current_view.nil? && options[:template]
      current_view[:locals] = options[:locals] if !current_view.nil? && options[:locals]
      if current_view.present?
        @view_stack ||= []
        @view_stack << current_view
      end
    end
    result = render_without_active_scaffold(*args, &block)
    @view_stack.pop if current_view.present?
    result
  end
end

#template_exists?(template_name, lookup_overrides = false) ⇒ Boolean

This is the template finder logic, keep it updated with however we find stuff in rails currently this very similar to the logic in ActionBase::Base.render for options file TODO: Work with rails core team to find a better way to check for this.

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
# File 'lib/active_scaffold/extensions/action_view_rendering.rb', line 113

def template_exists?(template_name, lookup_overrides = false)
  begin
    method = 'find_template'
    method << '_without_active_scaffold' unless lookup_overrides
    self.view_paths.send(method, template_name, @template_format)
    return true
  rescue ActionView::MissingTemplate => e
    return false
  end
end