Skip to content
Browse files

getting controllers working with region sets

updated the handling of recognizing paths for admin ui tab visibility
moving some details of responses from resource_responses into resource_controller
  • Loading branch information...
1 parent 2058f28 commit 2d1f7d6ff151644e4001691c2e4f25211d47e080 @saturnflyer saturnflyer committed Feb 19, 2013
View
11 app/controllers/radiant/admin/resource_controller.rb
@@ -2,7 +2,16 @@
require 'radiant/resource_responses'
module Radiant
class Admin::ResourceController < ::Radiant::AdminController
- extend Radiant::ResourceResponses
+ class_attribute :responses
+
+ def self.responses
+ @responses ||= Radiant::ResourceResponses::Collector.new
+ self.responses = @responses
+ yield @responses if block_given?
+ @responses
+ end
+
+ include Radiant::ResourceResponses::InstanceMethods
helper_method :model, :current_object, :models, :current_objects, :model_symbol, :plural_model_symbol, :model_class, :model_name, :plural_model_name
before_filter :populate_format
View
1 app/helpers/radiant/application_helper.rb
@@ -1,3 +1,4 @@
+require 'radiant/admin_ui'
module Radiant
module ApplicationHelper
include Radiant::RegionsHelper
View
5 app/helpers/radiant/regions_helper.rb
@@ -1,3 +1,4 @@
+require 'radiant/admin_ui/region_partials'
module Radiant::RegionsHelper
def render_region(region, options={}, &block)
lazy_initialize_region_set
@@ -20,8 +21,8 @@ def render_region(region, options={}, &block)
def lazy_initialize_region_set
unless @region_set
- @controller_name ||= @controller.controller_name
- @template_name ||= @controller.template_name
+ @controller_name ||= controller.controller_name
+ @template_name ||= controller.template_name
@region_set = admin.send(@controller_name).send(@template_name)
end
end
View
70 lib/radiant/admin_ui.rb
@@ -1,30 +1,30 @@
require 'simpleton'
require 'ostruct'
+require 'radiant/admin_ui/region_set'
+require 'radiant/admin_ui/region_partials'
module Radiant
class AdminUI
- # This may be loaded before ActiveSupport, so do an explicit require
- require 'radiant/admin_ui/region_set'
-
+ include Simpleton
class DuplicateTabNameError < StandardError; end
-
+
# The NavTab Class holds the structure of a navigation tab (including
# its sub-nav items).
class NavTab < Array
attr_reader :name
-
+
def initialize(name)
@name = name
end
-
+
def [](id)
unless id.kind_of? Fixnum
self.find {|subnav_item| subnav_item.name.to_s.titleize == id.to_s.titleize }
else
super
end
end
-
+
def <<(*args)
options = args.extract_options!
item = args.size > 1 ? deprecated_add(*(args << caller)) : args.first
@@ -46,9 +46,9 @@ def <<(*args)
end
end
end
-
+
alias :add :<<
-
+
def add_item(*args)
options = args.extract_options!
options.symbolize_keys!
@@ -63,37 +63,37 @@ def add_item(*args)
add NavSubItem.new(args.first, args.second)
end
end
-
+
def visible?(user)
any? { |sub_item| sub_item.visible?(user) }
end
-
+
def deprecated_add(name, url, caller)
ActiveSupport::Deprecation.warn("admin.tabs.add is no longer supported in Radiant 0.9.x. Please update your code to use: \ntab \"Content\" do\n\tadd_item(...)\nend", caller)
NavSubItem.new(name, url)
end
end
-
+
# Simple structure for storing the properties of a tab's sub items.
class NavSubItem
attr_reader :name, :url
attr_accessor :tab
-
+
def initialize(name, url = "#")
@name, @url = name, url
end
-
+
def visible?(user)
visible_by_controller?(user)
end
-
+
def relative_url
File.join(ActionController::Base.relative_url_root || '', url)
end
-
+
private
def visible_by_controller?(user)
- params = ActionController::Routing::Routes.recognize_path(url, :method => :get)
+ params = Radiant::Engine.routes.recognize_path(url, :method => :get)
if params && params[:controller]
klass = "#{params[:controller].camelize}Controller".constantize
klass.user_has_access_to_action?(user, params[:action])
@@ -102,66 +102,64 @@ def visible_by_controller?(user)
end
end
end
-
- include Simpleton
-
+
attr_accessor :nav
-
+
def nav_tab(*args)
NavTab.new(*args)
end
-
+
def nav_item(*args)
NavSubItem.new(*args)
end
-
+
def tabs
nav['Content']
end
-
+
# Region sets
%w{page layout user configuration extension}.each do |controller|
attr_accessor controller
alias_method "#{controller}s", controller
end
-
+
def initialize
initialize_nav
load_default_regions
end
-
+
def initialize_nav
@nav = NavTab.new("Tab Container")
load_default_nav
end
-
+
def load_default_nav
content = nav_tab("Content")
content << nav_item("Pages", "/admin/pages")
nav << content
-
+
design = nav_tab("Design")
design << nav_item("Layouts", "/admin/layouts")
nav << design
-
+
settings = nav_tab("Settings")
settings << nav_item("General", "/admin/configuration")
settings << nav_item("Personal", "/admin/preferences")
settings << nav_item("Users", "/admin/users")
settings << nav_item("Extensions", "/admin/extensions")
nav << settings
end
-
+
def load_default_regions
@page = load_default_page_regions
@layout = load_default_layout_regions
@user = load_default_user_regions
@configuration = load_default_configuration_regions
@extension = load_default_extension_regions
end
-
+
private
-
+
def load_default_page_regions
OpenStruct.new.tap do |page|
page.edit = RegionSet.new do |edit|
@@ -178,7 +176,7 @@ def load_default_page_regions
page.new = page._part = page.edit
end
end
-
+
def load_default_user_regions
OpenStruct.new.tap do |user|
user.preferences = RegionSet.new do |preferences|
@@ -200,7 +198,7 @@ def load_default_user_regions
user.new = user.edit
end
end
-
+
def load_default_layout_regions
OpenStruct.new.tap do |layout|
layout.edit = RegionSet.new do |edit|
@@ -217,7 +215,7 @@ def load_default_layout_regions
layout.new = layout.edit
end
end
-
+
def load_default_configuration_regions
OpenStruct.new.tap do |configuration|
configuration.show = RegionSet.new do |show|
@@ -231,7 +229,7 @@ def load_default_configuration_regions
end
end
end
-
+
def load_default_extension_regions
OpenStruct.new.tap do |extension|
extension.index = RegionSet.new do |index|
View
42 lib/radiant/admin_ui/region_partials.rb
@@ -1,23 +1,27 @@
-class Radiant::AdminUI::RegionPartials
- def initialize(template)
- @partials = Hash.new {|h,k| h[k] = "<strong>`#{k}' default partial not found!</strong>" }
- @template = template
- end
-
- def [](key)
- @partials[key.to_s]
- end
-
- def method_missing(method, *args, &block)
- if block_given?
- # Ruby 1.9.2 yields self in instance_eval... see https://gist.github.com/479572
- # lambdas are as strict as methods in 1.9.x, making sure that the args match, Procs are not.
- if RUBY_VERSION =~ /^1\.9/ and block.lambda? and block.arity != 1
- raise "You can only pass a proc ('Proc.new') or a lambda that takes exactly one arg (for self) to Radiant::AdminUI::RegionPartials' method_missing."
+module Radiant
+ class AdminUI
+ class RegionPartials
+ def initialize(template)
+ @partials = Hash.new {|h,k| h[k] = "<strong>`#{k}' default partial not found!</strong>" }
+ @template = template
+ end
+
+ def [](key)
+ @partials[key.to_s]
+ end
+
+ def method_missing(method, *args, &block)
+ if block_given?
+ # Ruby 1.9.2 yields self in instance_eval... see https://gist.github.com/479572
+ # lambdas are as strict as methods in 1.9.x, making sure that the args match, Procs are not.
+ if RUBY_VERSION =~ /^1\.9/ and block.lambda? and block.arity != 1
+ raise "You can only pass a proc ('Proc.new') or a lambda that takes exactly one arg (for self) to Radiant::AdminUI::RegionPartials' method_missing."
+ end
+ @partials[method.to_s] = @template.capture(&block)
+ else
+ @partials[method.to_s]
+ end
end
- @partials[method.to_s] = @template.capture(&block)
- else
- @partials[method.to_s]
end
end
end
View
66 lib/radiant/admin_ui/region_set.rb
@@ -1,35 +1,39 @@
-class Radiant::AdminUI::RegionSet
+module Radiant
+ class AdminUI
+ class RegionSet
+
+ def initialize
+ @regions = Hash.new do |h,k|
+ h[k] = []
+ end
+ yield self if block_given?
+ end
+
+ def [](region)
+ @regions[region.to_sym]
+ end
+
+ def add(region=nil, partial=nil, options={})
+ raise ArgumentError, "You must specify a region and a partial" unless region and partial
+ if options[:before]
+ index = @regions[region].empty? ? 0 : (@regions[region].index(options[:before]) || @regions[region].size)
+ self[region].insert(index, partial)
+ elsif options[:after]
+ index = @regions[region].empty? ? 0 : (@regions[region].index(options[:after]) || @regions[region].size - 1)
+ self[region].insert(index + 1, partial)
+ else
+ self[region] << partial
+ end
+ end
+
+ def method_missing(method, *args, &block)
+ if args.empty?
+ self[method]
+ else
+ super
+ end
+ end
- def initialize
- @regions = Hash.new do |h,k|
- h[k] = []
- end
- yield self if block_given?
- end
-
- def [](region)
- @regions[region.to_sym]
- end
-
- def add(region=nil, partial=nil, options={})
- raise ArgumentError, "You must specify a region and a partial" unless region and partial
- if options[:before]
- index = @regions[region].empty? ? 0 : (@regions[region].index(options[:before]) || @regions[region].size)
- self[region].insert(index, partial)
- elsif options[:after]
- index = @regions[region].empty? ? 0 : (@regions[region].index(options[:after]) || @regions[region].size - 1)
- self[region].insert(index + 1, partial)
- else
- self[region] << partial
- end
- end
-
- def method_missing(method, *args, &block)
- if args.empty?
- self[method]
- else
- super
end
end
-
end
View
11 lib/radiant/resource_responses.rb
@@ -1,17 +1,6 @@
require 'ostruct'
module Radiant
module ResourceResponses
- def self.extended(base)
- base.send :class_attribute, :responses
- base.send :include, InstanceMethods
- end
-
- def responses
- r = (self.responses = read_inheritable_attribute(:responses) || Collector.new)
- yield r if block_given?
- r
- end
-
module InstanceMethods
def response_for(action)
responses = self.class.responses.send(action)

0 comments on commit 2d1f7d6

Please sign in to comment.
Something went wrong with that request. Please try again.