listalicious 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "listalicious"
8
- gem.summary = %Q{Semantic lists (datagrids) for Rails}
8
+ gem.summary = %Q{Semantic lists (aka datagrids) for Rails}
9
9
  gem.description = %Q{Semantic listing; a semantic way to build datagrid structures in Rails.}
10
10
  gem.email = "jejacks0n@gmail.com"
11
11
  gem.homepage = "http://github.com/jejacks0n/listalicious"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -5,8 +5,10 @@ module Listalicious
5
5
 
6
6
  def initialize(template, collection, options = {}, &proc)
7
7
  @template, @collection, @options = template, collection, options
8
- @object_name = "#{options[:as] || collection.first ? collection.first.class.name.underscore : ''}"
8
+ @object_name = "#{options[:as] || (collection.empty? ? '' : collection.first.class.name.underscore)}"
9
9
  @table_name = (options[:table_name] || @object_name.pluralize).to_s
10
+ @object = @object_name.classify.constantize.new
11
+ @column_count = 0
10
12
 
11
13
  render(options.delete(:html), &proc)
12
14
  end
@@ -19,9 +21,15 @@ module Listalicious
19
21
  # eg. order[users][]=login:asc&order[users][]=first_name:asc
20
22
  def orderable_link(contents, field)
21
23
  return contents if @object_name.empty?
24
+ field = field.to_s
22
25
 
23
26
  order_params = (template.params['order'] || {})[@table_name]
24
27
  fields = Hash[*order_params.to_a.collect { |field_and_dir| field_and_dir.split(':') }.flatten]
28
+ if @object && @object.default_order[:field].to_s == field
29
+ # TODO: this should technically be fields.empty? && @object.default_order[:options][:stable],
30
+ # but it lends itself to a little bit of an odd behavior unless you're expecting the additive ordering
31
+ fields[field] = fields[field] || @object.default_order[:direction].to_s if fields.empty?
32
+ end
25
33
 
26
34
  order_params = (template.params['order'] || {}).clone.
27
35
  merge({@table_name => ["#{field}:#{"#{fields[field] == 'asc' ? 'desc' : 'asc'}"}"]})
@@ -41,7 +41,7 @@ module Listalicious
41
41
  def column_group_head(options = {}, &proc)
42
42
  @column_count = 0
43
43
 
44
- @head_wrapper = template.content_tag(:tr, template.capture(collection.first, 0, &proc),
44
+ @head_wrapper = template.content_tag(:tr, template.capture(collection.first || @object, 0, &proc),
45
45
  options[:html].merge({:class => template.add_class(options[:html][:class], 'header')}))
46
46
  template.content_tag(:thead, @head_wrapper, options.delete(:wrapper_html))
47
47
  end
@@ -78,7 +78,13 @@ module Listalicious
78
78
  contents = options == args.first ? nil : args.first
79
79
 
80
80
  if @current_scope == :body
81
- contents = template.capture(self, &proc) if block_given? && collection.first.present?
81
+ contents = if block_given?
82
+ template.capture(self, &proc)
83
+ elsif args.last.is_a?(Proc)
84
+ args.last.call
85
+ else
86
+ contents
87
+ end
82
88
  else
83
89
  contents = options[:title] || contents
84
90
  end
@@ -124,7 +130,8 @@ module Listalicious
124
130
  options[:html] ||= {}
125
131
  options[:html][:width] ||= options[:width]
126
132
 
127
- contents = contents.to_s.humanize.titleize if contents.is_a? Symbol
133
+ options[:sort] ||= contents.is_a?(Symbol) ? contents : nil
134
+ contents = contents.to_s.humanize.titleize
128
135
  contents = orderable_link(contents, options[:sort]) if options[:sort]
129
136
 
130
137
  template.content_tag(:th, contents, options.delete(:html))
data/lib/listalicious.rb CHANGED
@@ -2,11 +2,18 @@
2
2
  require 'builders/generic_builder'
3
3
  require 'builders/table_builder'
4
4
 
5
- module Listalicious #:nodoc:
5
+ module Listalicious
6
6
 
7
7
  # Semantic list helper methods
8
8
  #
9
- # Example Usage:
9
+ # == Example Usage (HAML)
10
+ # - semantic_list_for @users, :as => :user, :html => {:class => 'user-list'} do |l|
11
+ # = l.columns [:head, :body] do |user, index|
12
+ # = l.column "#{user.first_name} #{user.last_name}", :title => 'User Name', :sort => 'first_name', :width => '20%'
13
+ # = l.column :login, :width => '20%'
14
+ # = l.column link_to(user.email, "mailto:#{user.email}"), :title => 'Email Address', :sort => 'email', :width => '40%'
15
+ # = l.controls do
16
+ # = link_to('edit', edit_user_path(user))
10
17
  #
11
18
  module SemanticListHelper
12
19
 
@@ -20,7 +27,7 @@ module Listalicious #:nodoc:
20
27
 
21
28
  options[:html] ||= {}
22
29
  options[:html][:class] = add_class(options[:html][:class], 'semantic-list')
23
- options[:html][:id] ||= "#{options[:as] || collection.first ? collection.first.class.name.underscore : 'semantic'}_list"
30
+ options[:html][:id] ||= options[:as].to_s
24
31
 
25
32
  if options[:sort_url]
26
33
  options[:html][:class] = add_class(options[:html][:class], 'sortable')
@@ -30,7 +37,7 @@ module Listalicious #:nodoc:
30
37
  options[:html][:class] = add_class(options[:html][:class], 'selectable') if options[:selectable]
31
38
  options[:html][:class] = add_class(options[:html][:class], 'expandable') if options[:expandable]
32
39
 
33
- builder = options[:builder] || @@builder
40
+ builder = options[:builder] || TableBuilder
34
41
  builder.new(@template, collection, options, &proc)
35
42
  end
36
43
 
@@ -41,7 +48,45 @@ module Listalicious #:nodoc:
41
48
 
42
49
  end
43
50
 
44
- module ActiveRecordExtensions # :nodoc:
51
+ module ActiveRecordExtensions
52
+
53
+ # Makes a given model orderable for lists
54
+ #
55
+ # To specify that a model behaves according to the Listalicious order style call orderable_fields. The
56
+ # orderable_fields method takes a configuration block.
57
+ #
58
+ # === Example
59
+ # orderable_fields do
60
+ # only :first_name, :last_name
61
+ # default :last_name
62
+ # end
63
+ #
64
+ # === Configuration Methods
65
+ # [only]
66
+ # Provide fields that are orderable.
67
+ # [except]
68
+ # Provide fields that are not orderable, with the default list being all fields.
69
+ # [default]
70
+ # Provide the default sort field, optionally a direction, and additional options.
71
+ #
72
+ # *Notes*:
73
+ # * If +only+ or +except+ are not called within the block, all fields on the model will be orderable, this includes
74
+ # things like id, and password/password salt columns.
75
+ # * If +default+ isn't called, the first field will be considered the default, asc being the default direction.
76
+ #
77
+ def orderable_fields(&config_block)
78
+ cattr_accessor :orderable_fields, :default_order
79
+
80
+ # make all columns orderable, incase only or except aren't called in the configuration block
81
+ self.orderable_fields = column_names.map { |column_name| column_name.to_s }
82
+
83
+ OrderableConfiguration.new(self).instance_eval(&config_block)
84
+ self.orderable_fields.collect!{ |field| field.to_s }
85
+
86
+ self.default_order ||= {:field => self.orderable_fields.first, :direction => :desc, :options => {}}
87
+
88
+ attach_orderable_scopes
89
+ end
45
90
 
46
91
  class OrderableConfiguration
47
92
  def initialize(target)
@@ -83,48 +128,10 @@ module Listalicious #:nodoc:
83
128
  field = args.shift
84
129
  direction = args.shift || :asc
85
130
 
86
- @target.default_order = {:field => field, :direction => direction, :options => options}
131
+ @target.default_order = {:field => field, :direction => direction, :options => options || {}}
87
132
  end
88
133
  end
89
134
 
90
- # Makes a given model orderable for lists
91
- #
92
- # To specify that a model behaves according to the Listalicious order style call orderable_fields. The
93
- # orderable_fields method takes a configuration block.
94
- #
95
- # === Example
96
- # orderable_fields do
97
- # only :first_name, :last_name
98
- # default :last_name
99
- # end
100
- #
101
- # === Configuration Methods
102
- # [only]
103
- # Provide fields that are orderable.
104
- # [except]
105
- # Provide fields that are not orderable, with the default list being all fields.
106
- # [default]
107
- # Provide the default sort field, optionally a direction, and additional options.
108
- #
109
- # *Notes*:
110
- # * If +only+ or +except+ are not called within the block, all fields on the model will be orderable, this includes
111
- # things like id, and password/password salt columns.
112
- # * If +default+ isn't called, the first field will be considered the default, asc being the default direction.
113
- #
114
- def orderable_fields(&config_block)
115
- cattr_accessor :orderable_fields, :default_order
116
-
117
- # make all columns orderable, incase only or except aren't called in the configuration block
118
- self.orderable_fields = column_names.map { |column_name| column_name.to_s }
119
-
120
- OrderableConfiguration.new(self).instance_eval(&config_block)
121
- self.orderable_fields.collect!{ |field| field.to_s }
122
-
123
- self.default_order ||= {:field => self.orderable_fields.first, :direction => :desc}
124
-
125
- attach_orderable_scopes
126
- end
127
-
128
135
  # Attaches the ordered_from named scope to the model requesting it. The named scope can be chained in the
129
136
  # controller by using:
130
137
  #
@@ -137,16 +144,21 @@ module Listalicious #:nodoc:
137
144
  # Which will generate the order clause +"last_name DESC, first_name ASC"+.
138
145
  def attach_orderable_scopes
139
146
  self.named_scope :ordered_from, lambda { |params|
140
- return unless params.include?(:order) and params[:order][self.table_name.to_sym]
141
-
142
- fields = params[:order][self.table_name.to_sym].collect do |field_and_dir|
143
- field, dir = field_and_dir.split(':')
144
- self.orderable_fields.include?(field) ? [field, dir.to_s.downcase] : nil
145
- end.compact
146
-
147
+ fields = []
148
+ if params.include?(:order) and params[:order][self.table_name.to_sym]
149
+ fields = params[:order][self.table_name.to_sym].collect do |field_and_dir|
150
+ field, dir = field_and_dir.split(':')
151
+ self.orderable_fields.include?(field) ? [field, dir.to_s.downcase] : nil
152
+ end.compact
153
+ end
154
+
155
+ if self.default_order && fields.empty? || self.default_order[:options][:stable]
156
+ fields << [self.default_order[:field], self.default_order[:direction]]
157
+ end
158
+
147
159
  fields.empty? ?
148
160
  nil :
149
- {:order => fields.map{ |field, dir| "#{field} #{dir.downcase == 'desc' ? 'DESC' : 'ASC'}" }.join(', ')}
161
+ {:order => fields.map{ |field, dir| "#{field} #{dir.to_s.downcase == 'desc' ? 'DESC' : 'ASC'}" }.join(', ')}
150
162
  }
151
163
  end
152
164
 
data/listalicious.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{listalicious}
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremy Jackson"]
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
34
34
  s.rdoc_options = ["--charset=UTF-8"]
35
35
  s.require_paths = ["lib"]
36
36
  s.rubygems_version = %q{1.3.5}
37
- s.summary = %q{Semantic lists (datagrids) for Rails}
37
+ s.summary = %q{Semantic lists (aka datagrids) for Rails}
38
38
  s.test_files = [
39
39
  "test/helper.rb",
40
40
  "test/test_listalicious.rb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: listalicious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Jackson
@@ -71,7 +71,7 @@ rubyforge_project:
71
71
  rubygems_version: 1.3.5
72
72
  signing_key:
73
73
  specification_version: 3
74
- summary: Semantic lists (datagrids) for Rails
74
+ summary: Semantic lists (aka datagrids) for Rails
75
75
  test_files:
76
76
  - test/helper.rb
77
77
  - test/test_listalicious.rb