Sha256: 7facfaaec70745891e22a8f4829a9842fde7e7d3db7e3c9793df15ef90b93604

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

module BestInPlace
  module DisplayMethods
    extend self

    class Renderer < Struct.new(:opts)
      def render_json(object)
        case opts[:type]
        when :model
          {:display_as => object.send(opts[:method])}.to_json
        when :helper
          value = if opts[:helper_options]
                    BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]), opts[:helper_options])
                  else
                    BestInPlace::ViewHelpers.send(opts[:method], object.send(opts[:attr]))
                  end
          {:display_as => value}.to_json
        else
          {}.to_json
        end
      end
    end

    @@table = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }

    def lookup(klass, attr)
      foo = @@table[klass.to_s][attr.to_s]
      foo == {} ? nil : foo
    end

    def add_model_method(klass, attr, display_as)
      @@table[klass.to_s][attr.to_s] = Renderer.new :method => display_as.to_sym, :type => :model
    end

    def add_helper_method(klass, attr, helper_method, helper_options = nil)
      @@table[klass.to_s][attr.to_s] = Renderer.new :method => helper_method.to_sym, :type => :helper, :attr => attr, :helper_options => helper_options
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
best_in_place-1.1.2 lib/best_in_place/display_methods.rb
best_in_place-1.1.1 lib/best_in_place/display_methods.rb
best_in_place-1.1.0 lib/best_in_place/display_methods.rb
best_in_place-1.0.6 lib/best_in_place/display_methods.rb
best_in_place-1.0.5 lib/best_in_place/display_methods.rb
best_in_place-1.0.4 lib/best_in_place/display_methods.rb