plugin.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:6k
源码类别:

Ajax

开发平台:

Others

  1. module Sidebars
  2.   class Field
  3.     attr_accessor :key
  4.     attr_accessor :options
  5.     include ApplicationHelper
  6.     include ActionView::Helpers::TagHelper
  7.     include ActionView::Helpers::FormTagHelper
  8.     include ActionView::Helpers::FormOptionsHelper
  9.     def initialize(key = nil, options = { })
  10.       @key, @options = key.to_s, options
  11.     end
  12.     def label_html(sidebar)
  13.       content_tag('label', options[:label] || key.humanize.gsub(/url/i, 'URL'))
  14.     end
  15.     def input_html(sidebar)
  16.       text_field_tag(input_name(sidebar), sidebar.config[key], options)
  17.     end
  18.     def line_html(sidebar)
  19.       label_html(sidebar) +  "<br />" + input_html(sidebar) + "<br />"
  20.     end
  21.     def input_name(sidebar)
  22.       "configure[#{sidebar.id}][#{key}]"
  23.     end
  24.     class SelectField < self
  25.       def input_html(sidebar)
  26.         select_tag(input_name(sidebar),
  27.                    options_for_select(options[:choices], sidebar.config[key]),
  28.                    options)
  29.       end
  30.     end
  31.     class TextAreaField < self
  32.       def input_html(sidebar)
  33.         html_options = { "rows" => "10", "cols" => "30", "style" => "width:255px"}.update(options.stringify_keys)
  34.         text_area_tag(input_name(sidebar), h(sidebar.config[key]), html_options)
  35.       end
  36.     end
  37.     class RadioField < self
  38.       def input_html(sidebar)
  39.         options[:choices].collect do |choice|
  40.           value = value_for(choice)
  41.           radio_button_tag(input_name(sidebar), value,
  42.                            value == sidebar.config[key], options) +
  43.             content_tag('label', label_for(choice))
  44.         end.join("<br />")
  45.       end
  46.       def label_for(choice)
  47.         choice.is_a?(Array) ? choice.last : choice.to_s.humanize
  48.       end
  49.       def value_for(choice)
  50.         choice.is_a?(Array) ? choice.first : choice
  51.       end
  52.     end
  53.     class CheckBoxField < self
  54.       def input_html(sidebar)
  55.         check_box_tag(input_name(sidebar), 1, sidebar.config[key], options)+
  56.         hidden_field_tag(input_name(sidebar),0)
  57.       end
  58.       def line_html(sidebar)
  59.         input_html(sidebar) + ' ' + label_html(sidebar) + '<br >'
  60.       end
  61.     end
  62.     def self.build(key, options)
  63.       field = class_for(options).new(key, options)
  64.     end
  65.     def self.class_for(options)
  66.       case options[:input_type]
  67.       when :text_area
  68.         TextAreaField
  69.       when :textarea
  70.         TextAreaField
  71.       when :radio
  72.         RadioField
  73.       when :checkbox
  74.         CheckBoxField
  75.       when :select
  76.         SelectField
  77.       else
  78.         if options[:choices]
  79.           SelectField
  80.         else
  81.           self
  82.         end
  83.       end
  84.     end
  85.   end
  86.   class Sidebars::Plugin < ApplicationController
  87.     include ApplicationHelper
  88.     helper :theme
  89.     @@subclasses = { }
  90.     class << self
  91.       def inherited(child)
  92.         @@subclasses[self] ||= []
  93.         @@subclasses[self] |= [child]
  94.         super
  95.       end
  96.       def subclasses
  97.         @@subclasses[self] ||= []
  98.         @@subclasses[self] + extra =
  99.           @@subclasses[self].inject([]) {|list, subclass| list | subclass.subclasses }
  100.       end
  101.       def available_sidebars
  102.         Sidebars::Plugin.subclasses.select do |sidebar|
  103.           sidebar.concrete?
  104.         end
  105.       end
  106.       def concrete?
  107.         self.to_s !~ /^Sidebars::(Co(mponent|nsolidated))?Plugin/
  108.       end
  109.       # The name that's stored in the DB.  This is the final chunk of the
  110.       # controller name, like 'xml' or 'flickr'.
  111.       def short_name
  112.         self.to_s.underscore.split(%r{/}).last
  113.       end
  114.       @@display_name_of = { }
  115.       @@description_of  = { }
  116.       # The name that shows up in the UI
  117.       def display_name(new_dn = nil)
  118.         # This is the default, but it's best to override it
  119.         self.display_name = new_dn if new_dn
  120.         @@display_name_of[self] || short_name.humanize
  121.       end
  122.       def display_name=(name)
  123.         @@display_name_of[self] = name
  124.       end
  125.       def description(new_desc = nil)
  126.         self.description = new_desc if new_desc
  127.         @@description_of[self] || short_name.humanize
  128.       end
  129.       def description=(desc)
  130.         @@description_of[self] = desc
  131.       end
  132.       @@fields = { }
  133.       @@default_config = { }
  134.       def setting(key, default=nil, options={ })
  135.         fields << Field.build(key, options)
  136.         default_config[key.to_s] = default
  137.         self.send(:define_method, key) do
  138.           @sb_config[key.to_s]
  139.         end
  140.         self.helper_method(key)
  141.       end
  142.       def default_config
  143.         @@default_config[self] ||= HashWithIndifferentAccess.new
  144.       end
  145.       def default_config=(newval)
  146.         @@default_config[self] = newval
  147.       end
  148.       def fields
  149.         @@fields[self] ||= []
  150.       end
  151.       def fields=(newval)
  152.         @@fields[self] = newval
  153.       end
  154.       def default_helper_module!
  155.       end
  156.     end
  157.     def index
  158.       @sidebar=params['sidebar']
  159.       set_config
  160.       @sb_config = @sidebar.config
  161.       content
  162.       render :action=>'content' unless performed?
  163.     end
  164.     def configure_wrapper
  165.       @sidebar=params['sidebar']
  166.       set_config
  167.       configure
  168.       render :action=>'configure' unless performed?
  169.     end
  170.     # This controller is used on to actually display sidebar items.
  171.     def content
  172.     end
  173.     # This controller is used to configure the sidebar from /admin/sidebar
  174.     def configure
  175.       render :partial => 'sidebar/row', :collection => self.class.fields
  176.     end
  177.     private
  178.     def set_config
  179.       @sidebar.config ||= {}
  180.       @sidebar.config.reverse_merge!(self.class.default_config)
  181.     end
  182.     def sb_config(key)
  183.       config = @sidebar.class.default_config
  184.       config.merge!(@sidebar.config || {})
  185.       config[key.to_s]
  186.     end
  187.     # Horrid hack to make check_cache happy
  188.     def controller
  189.       self
  190.     end
  191.     def log_processing
  192.       logger.info "nnProcessing #{controller_class_name}##{action_name} (for #{request_origin})"
  193.     end
  194.   end
  195. end