datagrid 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -66,7 +66,7 @@ end
66
66
  Basic grid api:
67
67
 
68
68
  ``` ruby
69
- report = SimpleReport.new(:order => "group", :reverse => true, :group_id => [1,2], :from_logins_count => 1, :category => "first")
69
+ report = SimpleReport.new(:order => "group", :descending => true, :group_id => [1,2], :from_logins_count => 1, :category => "first")
70
70
 
71
71
  report.assets # => Array of User instances:
72
72
  # SELECT * FROM users WHERE users.group_id in (1,2) AND users.logins_count >= 1 AND users.category = 'first' ORDER BY groups.name DESC
@@ -111,7 +111,7 @@ Read more about filters here:
111
111
  ### Columns
112
112
 
113
113
  Each column is represented by name and code block to calculate the value.
114
- Grids are sortable by it's columns. Ordering is controller by `#order` and `#reverse` attributes.
114
+ Grids are sortable by it's columns. Ordering is controller by `#order` and `#descending` attributes.
115
115
 
116
116
  More information about columns here:
117
117
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datagrid}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bogdan Gusiev"]
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  "Gemfile.lock",
23
23
  "LICENSE.txt",
24
24
  "Rakefile",
25
- "Readme.md",
25
+ "Readme.markdown",
26
26
  "VERSION",
27
27
  "datagrid.gemspec",
28
28
  "lib/datagrid.rb",
@@ -42,6 +42,7 @@ Gem::Specification.new do |s|
42
42
  "lib/datagrid/form_builder.rb",
43
43
  "lib/datagrid/helper.rb",
44
44
  "lib/datagrid/rspec.rb",
45
+ "lib/datagrid/utils.rb",
45
46
  "spec/datagrid/columns_spec.rb",
46
47
  "spec/datagrid/filters_spec.rb",
47
48
  "spec/datagrid/form_builder_spec.rb",
@@ -1,3 +1,5 @@
1
+ require "datagrid/utils"
2
+
1
3
  module Datagrid
2
4
 
3
5
  class OrderUnsupported < StandardError
@@ -13,7 +15,9 @@ module Datagrid
13
15
  include Datagrid::Core
14
16
 
15
17
  datagrid_attribute :order
16
- datagrid_attribute :reverse
18
+ datagrid_attribute :descending do |value|
19
+ Datagrid::Utils.booleanize(value)
20
+ end
17
21
 
18
22
 
19
23
  end
@@ -63,26 +67,32 @@ module Datagrid
63
67
  # = Column value
64
68
  #
65
69
  # Column value can be defined by passing a block to <tt>#column</tt> method.
66
- # If no block given column is generated automatically by sending column name method to model.
70
+ # If no block given column it is generated automatically by sending column name method to model.
67
71
  # The block could have zero arguments(<tt>instance_eval</tt>) or one argument that is model object.
68
72
  #
73
+ # = Column options
74
+ #
75
+ # The following options can be passed to column definition:
76
+ #
77
+ # * <tt>:order</tt> - an order SQL that should be used to sort by this column.
78
+ # Default: report column name if there is database column with this name.
79
+ # * <tt>:order_desc</tt> - descending order expression from this column. Default: "#{order} desc".
80
+ #
81
+ # TODO: frontend options description
82
+ #
69
83
  # = Columns order
70
84
  #
71
85
  # Each column supports <tt>:order</tt> option that is used to specify SQL to sort data by the given column.
72
- # In order to specify order for the given grid the following attributes are used:
86
+ # In order to specify order the following attributes are used:
73
87
  #
74
- # * <tt>:order</tt> - column name to use order. Default: nil.
75
- # * <tt>:reverse</tt> - if true descending suffix is added to specified order. Default: false.
88
+ # * <tt>:order</tt> - column name to sort with as <tt>Symbol</tt>. Default: nil.
89
+ # * <tt>:descending</tt> - if true descending suffix is added to specified order. Default: false.
76
90
  #
77
- #
78
91
  # Example:
79
92
  #
80
- # grid = UserGrid.new(:order => :group, :reverse => true)
93
+ # grid = UserGrid.new(:order => :group, :descending => true)
81
94
  # grid.assets # => Return assets ordered by :group column descending
82
95
  #
83
- # = Options
84
- #
85
- # TODO
86
96
  def column(name, options = {}, &block)
87
97
  check_scope_defined!("Scope should be defined before columns")
88
98
  block ||= lambda do |model|
@@ -135,7 +145,7 @@ module Datagrid
135
145
  if self.order
136
146
  column = column_by_name(self.order)
137
147
  raise Datagrid::OrderUnsupported, "Can not sort #{self.inspect} by #{name.inspect}" unless column
138
- result = result.order(self.reverse ? column.desc_order : column.order)
148
+ result = result.order(self.descending ? column.desc_order : column.order)
139
149
  end
140
150
  result
141
151
  end
@@ -32,6 +32,7 @@ module Datagrid
32
32
 
33
33
  def datagrid_header(grid, options = {})
34
34
  header = empty_string
35
+ options[:order] = true unless options.has_key?(:order)
35
36
  grid.columns.each do |column|
36
37
  data = _safe(column.header)
37
38
  if options[:order] && column.order
@@ -60,7 +61,7 @@ module Datagrid
60
61
  :class => "order asc"
61
62
  ) + " " + link_to(
62
63
  I18n.t("datagrid.table.order.desc", :default => "DESC"),
63
- url_for(grid.param_name => grid.attributes.merge(:order => column.name, :reverse => true )),
64
+ url_for(grid.param_name => grid.attributes.merge(:order => column.name, :descending => true )),
64
65
  :class => "order desc"
65
66
  )
66
67
  end
@@ -21,8 +21,8 @@ shared_examples_for "Datagrid" do
21
21
  subject.assets.first.should_not be_nil
22
22
  end
23
23
 
24
- it "should support reverse order" do
25
- subject.reverse = true
24
+ it "should support descending order" do
25
+ subject.descending = true
26
26
  subject.assets.first.should_not be_nil
27
27
  end
28
28
  end
@@ -0,0 +1,12 @@
1
+ module Datagrid
2
+ module Utils
3
+ class << self
4
+
5
+ TRUTH =["1", true, 1, "true", "yes"]
6
+
7
+ def booleanize(value)
8
+ TRUTH.include?(value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -26,7 +26,7 @@ describe Datagrid do
26
26
 
27
27
  describe ".attributes" do
28
28
  it "should return report attributes" do
29
- (subject.filters.map(&:name) + [:order, :reverse]).each do |attribute|
29
+ (subject.filters.map(&:name) + [:order, :descending]).each do |attribute|
30
30
  subject.attributes.should have_key(attribute)
31
31
  end
32
32
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagrid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bogdan Gusiev
@@ -211,7 +211,7 @@ files:
211
211
  - Gemfile.lock
212
212
  - LICENSE.txt
213
213
  - Rakefile
214
- - Readme.md
214
+ - Readme.markdown
215
215
  - VERSION
216
216
  - datagrid.gemspec
217
217
  - lib/datagrid.rb
@@ -231,6 +231,7 @@ files:
231
231
  - lib/datagrid/form_builder.rb
232
232
  - lib/datagrid/helper.rb
233
233
  - lib/datagrid/rspec.rb
234
+ - lib/datagrid/utils.rb
234
235
  - spec/datagrid/columns_spec.rb
235
236
  - spec/datagrid/filters_spec.rb
236
237
  - spec/datagrid/form_builder_spec.rb