Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 155ae18b8dd9f0916bda442dacdb6c4c0073d1e1 @linojon committed Dec 26, 2009
13 README
@@ -0,0 +1,13 @@
+Gridify
+=======
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license
@@ -0,0 +1,11 @@
+require 'rake'
+require 'spec/rake/spectask'
+
+desc 'Default: run specs.'
+task :default => :spec
+
+desc 'Run the specs'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
@@ -0,0 +1,5 @@
+$:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
+
+Autotest.add_discovery do
+ "rspec"
+end
@@ -0,0 +1 @@
+require 'gridify'
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,44 @@
+require 'gridify/grid'
+
+module Gridify
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ attr_accessor :grids
+
+ def gridify(*args, &block)
+ # debugger
+ grid = Gridify::Grid.new( self, *args, &block)
+ @grids ||= {}
+ @grids[grid.name.to_sym] = grid
+
+ unless self.respond_to?(:find_for_grid)
+ class_eval <<-EOV
+ named_scope :find_for_grid, lambda {|name, params|
+ grid = grids[name]
+ grid.update_from_params( params )
+ grid.current_scope
+ }
+ EOV
+ end
+ end
+
+ def grids
+ @grids || {}
+ end
+
+ def grid
+ grids[:grid]
+ end
+
+
+ end
+end
+
+class ActiveRecord::Base
+ include Gridify
+end
+
@@ -0,0 +1,37 @@
+# lifted from state_machine plugin
+module Gridify
+ # Provides a set of helper methods for making assertions about the content
+ # of various objects
+ module Assertions
+ # Validates that the given hash *only* includes the specified valid keys.
+ # If any invalid keys are found, an ArgumentError will be raised.
+ #
+ # == Examples
+ #
+ # options = {:name => 'John Smith', :age => 30}
+ #
+ # assert_valid_keys(options, :name) # => ArgumentError: Invalid key(s): age
+ # assert_valid_keys(options, 'name', 'age') # => ArgumentError: Invalid key(s): age, name
+ # assert_valid_keys(options, :name, :age) # => nil
+ def assert_valid_keys(hash, *valid_keys)
+ invalid_keys = hash.keys - valid_keys
+ raise ArgumentError, "Invalid key(s): #{invalid_keys.join(', ')}" unless invalid_keys.empty?
+ end
+
+ # Validates that the given hash only includes at *most* one of a set of
+ # exclusive keys. If more than one key is found, an ArgumentError will be
+ # raised.
+ #
+ # == Examples
+ #
+ # options = {:only => :on, :except => :off}
+ # assert_exclusive_keys(options, :only) # => nil
+ # assert_exclusive_keys(options, :except) # => nil
+ # assert_exclusive_keys(options, :only, :except) # => ArgumentError: Conflicting keys: only, except
+ # assert_exclusive_keys(options, :only, :except, :with) # => ArgumentError: Conflicting keys: only, except
+ def assert_exclusive_keys(hash, *exclusive_keys)
+ conflicting_keys = exclusive_keys & hash.keys
+ raise ArgumentError, "Conflicting keys: #{conflicting_keys.join(', ')}" unless conflicting_keys.length <= 1
+ end
+ end
+end
@@ -0,0 +1,145 @@
+require 'gridify/assertions'
+require 'gridify/grid_options'
+require 'gridify/grid_view'
+require 'gridify/grid_column'
+require 'gridify/grid_finder'
+
+# NOTE: for readablity/clarity, native jqGrid options should be string; Ruby ones as syms
+
+module Gridify
+ class Grid
+ include Assertions
+
+ # todo: change this so klass is optional, decouple from active record
+ def initialize( klass, *args, &block )
+ #debugger
+ options = args.extract_options! # => args.last.is_a?(Hash) ? args.pop : {}
+ assert_exclusive_keys( options, :only, :except)
+
+ @resource = klass.to_s.tableize.pluralize
+ @name = args.first || :grid
+
+ # non-persistent options
+ # generate model unless explicitly say no
+ build_model = options.delete(:build_model) == false ? false : true
+ only = options.delete(:only)
+ except = options.delete(:except)
+ col_presets = options.delete(:columns)
+
+ # assign options
+ update options
+
+ # build columns from ActiveRecord model (klass)
+ if klass.present? && build_model
+ @model = build_columns klass, only, except, col_presets
+ end
+
+ instance_eval(&block) if block
+ #(note instance_eval cannot access things outside its scope; otherwise do this:
+ #yield self if block_given?
+ end
+
+ def update( options )
+ options.each {|atr, val| send( "#{atr}=", val )}
+ # exception "invalid option..."
+ end
+
+ def column( name, options={} )
+ name = name.to_s
+ klass = resource.classify.constantize
+ if col = columns_hash[name]
+ # update an existing column
+ col.update options
+
+ elsif ar = klass.columns.detect {|c| c.name==name}
+ debugger
+ # create column from database schema
+ edit = editable &&
+ # only edit accessible attributes
+ (klass.accessible_attributes.nil? || klass.accessible_attributes.include?(ar.name))
+ args = {
+ :ar_column => ar,
+ :name => ar.name,
+ :value_type => ar.type,
+ :searchable => searchable,
+ :sortable => sortable,
+ :editable => edit
+ }.merge(options)
+ columns << GridColumn.new( args)
+
+ else
+ # create column from scratch
+ args = {
+ :name => name,
+ :value_type => :string,
+ :searchable => searchable,
+ :sortable => sortable,
+ :editable => edit
+ }.merge(options)
+ columns << GridColumn.new( args)
+ end
+ end
+
+ def column_names
+ columns.collect {|col| col.name.titleize }
+ end
+
+ def column_model
+ columns.collect {|col| col.properties }
+ end
+
+ # normally we need to keep columns an ordered array, sometimes its convenient to have a hash
+ def columns_hash
+ columns.inject({}) {|h, col| h[col.name] = col; h }
+ end
+
+ protected
+
+ # # isnt there something in rails to do this already?
+ # def parse_options( keys, options )
+ # ops = keys.inject({}) do |h, k|
+ # val = options.delete(k)
+ # h[k] = val unless val.nil?
+ # h
+ # end
+ # ops || {}
+ # end
+
+ # generate list of columns based on AR model
+ # option: :only or :except
+ # :col_options hash of hash of preset values for columns (eg from cookie) { :title => {:width => 98}}
+ def build_columns( klass, only, except, presets )
+ #debugger
+ # stringify
+ only = Array(only).map {|s| s.to_s }
+ except = Array(except).map {|s| s.to_s }
+ presets ||= {}
+ presets.stringify_keys!
+
+ self.columns = klass.columns.collect do |ar|
+ #debugger
+ next if only.present? && !only.include?(ar.name)
+ next if except.present? && except.include?(ar.name)
+ is_key = (ar.name=='id')
+ edit = editable && !is_key &&
+ # only edit accessible attributes
+ (klass.accessible_attributes.nil? || klass.accessible_attributes.include?(ar.name))
+ args = {
+ :ar_column => ar,
+ :name => ar.name,
+ :value_type => ar.type,
+ :key => is_key,
+ :hidden => is_key,
+ :searchable => searchable,
+ :sortable => sortable,
+ :editable => edit
+ }
+
+ # create column with default args merged with options given for this column
+ GridColumn.new args.merge( presets[ar.name]||{} )
+ end.compact
+ end
+
+
+ end
+end
Oops, something went wrong.

0 comments on commit 155ae18

Please sign in to comment.