Sha256: 719876c694aad2972217c18500710ce90358cf7dcae3ae06a31c74918db8c5fa

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

module Hobo
  
  class ViewHints
    
    def self.enable
      ActiveSupport::Dependencies.load_paths |= ["#{RAILS_ROOT}/app/viewhints"]
    end
  
    def self.setter(name, default=nil, &block)
      ivname = name.to_s.remove(/\?$/)
      metaclass.send :define_method, name do |*args|
        if args.empty?
          val = instance_variable_get("@#{ivname}")
          if val.nil?
            val = default.is_a?(Proc) ? instance_eval(&default) : default
            instance_variable_set("@#{ivname}", val)
          end
          val
        else 
          arg = if block
                  block[*args] 
                else
                  args.first
                end
          instance_variable_set("@#{ivname}", arg)
        end
      end
    end
  
    setter :model_name,  proc { name.sub(/Hints$/, "") }
    
    setter :field_names, {}
    
    setter :field_help,  {}
    
    setter :children,    proc { model.dependent_collections.sort_by(&:to_s) } do |*args|
      args
    end
    
    setter :paginate?,    proc { !sortable? }
    
    setter :sortable?,    proc { defined?(ActiveRecord::Acts::List::InstanceMethods) && 
                                 model < ActiveRecord::Acts::List::InstanceMethods &&
                                 model.new.try.scope_condition == "1 = 1" }

    
    # Accessors
    
    class << self
      
      def model
        @model ||= name.sub(/Hints$/, "").constantize
      end
        

      def field_name(field)
        field_names.fetch(field.to_sym, field.to_s.titleize)
      end
    
      def primary_children
        children.first
      end
      
      def secondary_children
        children.rest
      end
      
    end
    
  end
  
end    

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hobo-0.8.10 lib/hobo/view_hints.rb
hobo-0.8.9 lib/hobo/view_hints.rb
hobo-0.8.8 lib/hobo/view_hints.rb
hobo-0.8.6 lib/hobo/view_hints.rb
hobo-0.8.7 lib/hobo/view_hints.rb