margrid 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83c794d5429839e68bb60d5d011985d7acff150a
4
- data.tar.gz: 6f127550111511be4d2526f6dce44c06acc8166c
3
+ metadata.gz: 5e90a0b5f874aa36fc69832915335874d23439fb
4
+ data.tar.gz: 28dc551ce26acab62637f2b55cbd2e8834346806
5
5
  SHA512:
6
- metadata.gz: 0ecc919ddac29945a2074bce80bababd284b1af65defbcac8f5bcec7dd84372d25fb2a8821ce9abf3293a3182510b49ec1cb30ed730d123e7e0a83929ccb46ad
7
- data.tar.gz: dc936ac2a2ccf3a572a41e44290ea725b18b4fa6d90c4223bb04f531321117ef654d28fc896955b0ea398c29b259510e4c510998c177d9d5636b6facd10509eb
6
+ metadata.gz: 6bff65b47a26f5c836cf5a518697b4de0be7844763aa158391476fd13024a52f41613d7eee1f48f8b29010cb044670b39442a7ccc8d752c207412cbffa159c40
7
+ data.tar.gz: 65ed641050d70973b41d4b1df672d386aefc6e881c29a1c6abf9e04338cbac3523d7d123917b8e0a5a0545189c2d409dd94ce38d4a27429c12b438fffac8b5aa
@@ -21,11 +21,11 @@ module Margrid
21
21
  end
22
22
 
23
23
  def self.load(data)
24
- new data[:sort], data[:direction] if data.key? :sort
24
+ new data["sort"], data["direction"] if data.key? "sort"
25
25
  end
26
26
 
27
27
  def dump
28
- {sort: column, direction: direction}
28
+ {"sort" => column, "direction" => direction}
29
29
  end
30
30
 
31
31
  private
@@ -43,11 +43,11 @@ module Margrid
43
43
  end
44
44
 
45
45
  def self.load(data)
46
- new data[:page] if data.key? :page
46
+ new data["page"] if data.key? "page"
47
47
  end
48
48
 
49
49
  def dump
50
- {page: current_page}
50
+ {"page" => current_page}
51
51
  end
52
52
  end
53
53
  end
@@ -3,9 +3,16 @@ module Margrid
3
3
  def initialize(id, relation)
4
4
  @id = id
5
5
  @relation = relation
6
+ @registered_components = {sorter: Sorter, paginator: Paginator}
6
7
  @components = {}
7
8
  end
8
9
 
10
+ def register(comp_sym, comp_class, comp = nil)
11
+ @registered_components[comp_sym] = comp_class
12
+ @components[comp_sym] = comp unless comp.nil?
13
+ self
14
+ end
15
+
9
16
  # Check wether a component is registered or not.
10
17
  def component?(comp_sym)
11
18
  @components.key? comp_sym
@@ -28,11 +35,10 @@ module Margrid
28
35
  # Load components from a Hash.
29
36
  def load(params)
30
37
  grid_params = params.fetch("margrid", {}).fetch(@id, {})
31
- if sorter = Sorter.load(grid_params)
32
- prepend sorter: sorter
33
- end
34
- if paginator = Paginator.load(grid_params)
35
- prepend paginator: paginator
38
+ @registered_components.each do |comp_sym, comp_class|
39
+ if comp = comp_class.load(grid_params)
40
+ prepend comp_sym => comp
41
+ end
36
42
  end
37
43
  self
38
44
  end
@@ -49,10 +55,9 @@ module Margrid
49
55
  # Dump components to a Hash. The key of the Hash is the rack param name.
50
56
  def to_query(comps = nil)
51
57
  comps ||= @components.values
52
- prefix = "margrid[#{@id}]"
53
58
  comps.inject({}) do |params, comp|
54
59
  comp.dump.each do |k, v|
55
- params[prefix + "[#{k}]"] = v
60
+ params[param_prefix + "[#{k}]"] = v
56
61
  end
57
62
  params
58
63
  end
@@ -66,5 +71,9 @@ module Margrid
66
71
  comp.apply(relation)
67
72
  end
68
73
  end
74
+
75
+ def param_prefix
76
+ @param_prefix ||= "margrid[#{@id}]"
77
+ end
69
78
  end
70
79
  end
@@ -1,3 +1,3 @@
1
1
  module Margrid
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -16,7 +16,7 @@ class BuilderTest < Margrid::TestCase
16
16
  end
17
17
 
18
18
  def test_load_components_from_params
19
- grid = @builder.load({"margrid" => {"built" => {page: 4}}})
19
+ grid = @builder.load({"margrid" => {"built" => {"page" => 4}}})
20
20
  assert_equal Margrid.paginator(4), grid.component(:paginator)
21
21
  end
22
22
 
@@ -6,8 +6,12 @@ class GridTest < Margrid::TestCase
6
6
  relation.do_stuff
7
7
  end
8
8
 
9
+ def self.load(params)
10
+ new params["prefix"]
11
+ end
12
+
9
13
  def dump
10
- {prefix => "state"}
14
+ {"prefix" => prefix}
11
15
  end
12
16
  end
13
17
 
@@ -16,6 +20,10 @@ class GridTest < Margrid::TestCase
16
20
  @grid = Margrid::Grid.new("test", @relation)
17
21
  end
18
22
 
23
+ def test_param_prefix
24
+ assert_equal "margrid[test]", @grid.param_prefix
25
+ end
26
+
19
27
  def test_prepending_components_overwrites_existing_ones
20
28
  @grid.prepend custom: (first = CustomComponent.new)
21
29
  @grid.prepend custom: (second = CustomComponent.new)
@@ -53,11 +61,11 @@ class GridTest < Margrid::TestCase
53
61
  @grid.prepend first: CustomComponent.new("some")
54
62
  @grid.prepend second: CustomComponent.new("more")
55
63
 
56
- assert_equal({"margrid"=>{"test"=>{"some"=>"state", "more"=>"state"}}}, @grid.dump)
64
+ assert_equal({"margrid"=>{"test"=>{"prefix"=>"some", "prefix"=>"more"}}}, @grid.dump)
57
65
  end
58
66
 
59
67
  def test_dumping_a_specific_component
60
- assert_equal({"margrid"=>{"test"=>{"not_much"=>"state"}}},
68
+ assert_equal({"margrid"=>{"test"=>{"prefix"=>"not_much"}}},
61
69
  @grid.dump([CustomComponent.new("not_much")]))
62
70
  end
63
71
 
@@ -65,26 +73,40 @@ class GridTest < Margrid::TestCase
65
73
  @grid.prepend first: CustomComponent.new("first")
66
74
  @grid.prepend second: CustomComponent.new("second")
67
75
 
68
- assert_equal({"margrid[test][first]"=>"state", "margrid[test][second]"=>"state"},
76
+ assert_equal({"margrid[test][prefix]"=>"first", "margrid[test][prefix]"=>"second"},
69
77
  @grid.to_query)
70
78
  end
71
79
 
72
80
  def test_representing_a_component_as_query
73
- assert_equal({"margrid[test][stuff]"=>"state"},
81
+ assert_equal({"margrid[test][prefix]"=>"stuff"},
74
82
  @grid.to_query([CustomComponent.new("stuff")]))
75
83
  end
76
84
 
77
85
  def test_load_components_form_a_hash
78
- @grid.load({"margrid" => {"test" => {sort: "name", direction: "asc", page: 3}}})
86
+ @grid.load({"margrid" => {"test" => {"sort" => "name", "direction" => "asc", "page" => 3}}})
79
87
  assert_equal Margrid.sorter("name", "asc"), @grid.component(:sorter)
80
88
  assert_equal Margrid.paginator(3), @grid.component(:paginator)
81
89
  end
82
90
 
83
91
  def test_load_and_prepend_overwrite_eachother
84
92
  @grid.prepend(paginator: CustomComponent.new("fluffy"))
85
- @grid.load({"margrid" => {"test" => {sort: "name", direction: "asc", page: 8}}})
93
+ @grid.load({"margrid" => {"test" => {"sort" => "name", "direction" => "asc", "page" => 8}}})
86
94
  @grid.prepend(sorter: CustomComponent.new("cat"))
87
95
  assert_equal CustomComponent.new("cat"), @grid.component(:sorter)
88
96
  assert_equal Margrid.paginator(8), @grid.component(:paginator)
89
97
  end
98
+
99
+ def test_load_with_custom_component
100
+ assert_equal @grid, @grid.register(:my_comp, CustomComponent)
101
+ assert_nil @grid.component(:my_comp)
102
+ @grid.load({"margrid" => {"test" => { "prefix" => "welcome" }}})
103
+ assert_equal CustomComponent.new("welcome"), @grid.component(:my_comp)
104
+ end
105
+
106
+ def test_register_component_with_default
107
+ assert_equal @grid, @grid.register(:my_comp, CustomComponent, CustomComponent.new("default"))
108
+ assert_equal CustomComponent.new("default"), @grid.component(:my_comp)
109
+ @grid.load({"margrid" => {"test" => { "prefix" => "welcome" }}})
110
+ assert_equal CustomComponent.new("welcome"), @grid.component(:my_comp)
111
+ end
90
112
  end
@@ -10,7 +10,7 @@ class PaginatorTest < Margrid::TestCase
10
10
  end
11
11
 
12
12
  def test_load_from_hash_with_existing_param
13
- paginator = Margrid::Paginator.load({page: 23})
13
+ paginator = Margrid::Paginator.load({"page" => 23})
14
14
  assert_equal Margrid.paginator(23), paginator
15
15
  end
16
16
 
@@ -21,6 +21,6 @@ class PaginatorTest < Margrid::TestCase
21
21
 
22
22
  def test_dumping_to_a_hash
23
23
  paginator = Margrid.paginator(7)
24
- assert_equal({page: 7}, paginator.dump)
24
+ assert_equal({"page" => 7}, paginator.dump)
25
25
  end
26
26
  end
@@ -10,7 +10,7 @@ class SorterTest < Margrid::TestCase
10
10
  end
11
11
 
12
12
  def test_load_from_hash_with_existing_param
13
- sorter = Margrid::Sorter.load({sort: "name", direction: "asc"})
13
+ sorter = Margrid::Sorter.load({"sort" => "name", "direction" => "asc"})
14
14
  assert_equal Margrid.sorter("name", "asc"), sorter
15
15
  end
16
16
 
@@ -21,7 +21,7 @@ class SorterTest < Margrid::TestCase
21
21
 
22
22
  def test_dumping_to_a_hash
23
23
  sorter = Margrid.sorter("author", "desc")
24
- assert_equal({sort: "author", direction: "desc"}, sorter.dump)
24
+ assert_equal({"sort" => "author", "direction" => "desc"}, sorter.dump)
25
25
  end
26
26
 
27
27
  def test_works_with_symbols_and_strings
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: margrid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yves Senn
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-08 00:00:00.000000000 Z
12
+ date: 2014-08-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler