crudle 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: b085645d20a6a26aa76a02f06b7ffdeaecbf236d
4
- data.tar.gz: e233fac2137bde5dddfd95a81b31c1cdb7b98e3f
3
+ metadata.gz: 518edfc85e64b5299f952a7985ff74a0e0d09da5
4
+ data.tar.gz: bc3c58be4766df258be5a65e323049baf70e13a3
5
5
  SHA512:
6
- metadata.gz: 52a40b6a01a03ad2c45cf1a709883a050fd43a1596e48e82116487ed00afea8e358d6d5953f12485cb3109eb0908c84aeb1396ab08575f6133530e8073dd0c0d
7
- data.tar.gz: 5e991bd543f35f82a4f35307a952217094fba3969f1c52527074b67b31d21b8a5a4a4bf5e08b438551cd1362f2ba6fc06aa5fd2a3076a8f55a589d02e6a32a7a
6
+ metadata.gz: 2afa5a283d9ae520bad351d8b72753428d623fba958367bd3b88f3923fe0bc7ae5c94e7ec4934d750241dc47620ad5d6ba42d9d4bd6f66f26a997fca1f5e41b2
7
+ data.tar.gz: 561a590e9fe3f83e177dee490483a6c26ee34e25e3ae5fc7f3b487f32e2d89f13591be2e1a363608df2d20aeb1f24f69caa18610eec6fa8a484188cfbe66eaea
data/crudle.gemspec CHANGED
@@ -1,10 +1,9 @@
1
1
  # coding: utf-8
2
+ require File.expand_path('../lib/crudle/version', __FILE__)
2
3
 
3
4
  Gem::Specification.new do |spec|
4
- spec.name, spec.version = %w[
5
- crudle
6
- 0.0.1
7
- ]
5
+ spec.name = 'crudle'
6
+ spec.version = Crudle::VERSION
8
7
  spec.authors = ['Slee Woo']
9
8
  spec.email = ['mail@sleewoo.com']
10
9
  spec.summary = [spec.name, spec.version]*'-',
@@ -0,0 +1,3 @@
1
+ module Crudle
2
+ VERSION = '0.0.3'.freeze
3
+ end
data/lib/crudle.rb CHANGED
@@ -1,14 +1,188 @@
1
- require 'crudle/controller_setup'
2
- require 'crudle/instance_methods'
3
-
4
1
  module Crudle
5
2
  NAME_QUERY = 'name ILIKE ?'.freeze
6
3
  NAME_QUERY_FORMAT = '%s%%'.freeze
7
- FILTER_METHOD_FORMAT = 'filter_by_%s'.freeze
4
+ FILTER_METHOD_FORMAT = '%s_filter'.freeze
8
5
 
9
6
  module Controller
10
7
  def self.included base
11
- base.class_exec(&Crudle::ControllerSetup)
8
+ base.class_exec do
9
+
10
+ def init params
11
+ {}
12
+ end
13
+
14
+
15
+ def list params = {}
16
+ dataset = filtered_dataset(params)
17
+ offset, pager = pager(params, dataset.count, items_per_page, side_pages)
18
+ {
19
+ items: fetch_items(dataset, offset, params[:id]).map {|i| {self: list_serializer(i)}},
20
+ pager: pager
21
+ }
22
+ end
23
+
24
+
25
+ def list_item item_id
26
+ list_serializer(find_item(item_id))
27
+ end
28
+
29
+
30
+ def create
31
+ model.create.id
32
+ end
33
+
34
+
35
+ def retrieve id
36
+ editor_serializer(find_item(id))
37
+ end
38
+
39
+
40
+ def update id, data
41
+ item = find_item(id)
42
+ data.each_pair {|k,v| item[k] = v}
43
+ item.valid? || halt(400, error: item.errors.each_with_object([]) {|e,o| o << e.join(': ')})
44
+ item.save
45
+ editor_serializer(item)
46
+ end
47
+
48
+
49
+ def delete id, params
50
+ find_item(id).destroy
51
+ list(params)
52
+ end
53
+
54
+
55
+ def delete_selected selected, params
56
+ model.where(id: selected).map(&:destroy)
57
+ list(params)
58
+ end
59
+
60
+
61
+ private
62
+
63
+ def find_item id
64
+ model.find_by(id: id) || halt(400, 'No item found by given ID: #%s' % escape_html(id.to_s))
65
+ end
66
+
67
+
68
+ def model
69
+ raise('Model not set. Please define a #model method that returns your model')
70
+ end
71
+
72
+
73
+ def items_per_page
74
+ 25
75
+ end
76
+
77
+
78
+ def side_pages
79
+ 4
80
+ end
81
+
82
+
83
+ def order
84
+ nil
85
+ end
86
+
87
+
88
+ def list_serializer item
89
+ serializer(item)
90
+ end
91
+
92
+
93
+ def editor_serializer item
94
+ serializer(item)
95
+ end
96
+
97
+
98
+ def serializer item
99
+ item
100
+ end
101
+
102
+
103
+ def filter params, dataset
104
+ return if params[:q].nil? || params[:q].empty?
105
+ if id = params[:q].scan(/id=(\d+)/).flatten[0]
106
+ return dataset.where(id: id)
107
+ end
108
+ dataset.where(Crudle::NAME_QUERY, Crudle::NAME_QUERY_FORMAT % params[:q])
109
+ end
110
+
111
+
112
+ def filtered_dataset params
113
+ filters = private_methods
114
+ params = indifferent_params(params)
115
+ params.inject(filter(params, model) || model) do |dataset,(k,v)|
116
+ filter_method = Crudle::FILTER_METHOD_FORMAT % k
117
+ if filters.include?(filter_method.to_sym)
118
+ send(filter_method, params, dataset) || dataset
119
+ else
120
+ dataset
121
+ end
122
+ end
123
+ end
124
+
125
+
126
+ def fetch_items dataset, offset, id = nil
127
+
128
+ items = if order = order()
129
+ model.unscoped do
130
+ dataset.order(order).offset(offset).limit(items_per_page).to_a
131
+ end
132
+ else
133
+ dataset.offset(offset).limit(items_per_page).to_a
134
+ end
135
+
136
+ if id
137
+ id = id.to_i
138
+ if !items.find {|i| i.id == id} && item = model.find_by(id: id)
139
+ items.delete_if {|i| i.id == item.id}
140
+ items.unshift(item)
141
+ end
142
+ end
143
+
144
+ items
145
+ end
146
+
147
+
148
+ def pager params, total_items, items_per_page, side_pages
149
+ total_pages = (total_items.to_f / items_per_page.to_f).ceil
150
+
151
+ current_page = params[:p].to_i
152
+ current_page = 1 if current_page < 1
153
+ current_page = total_pages if current_page > total_pages
154
+
155
+ next_page = current_page + 1
156
+ next_page = nil if next_page > total_pages
157
+ prev_page = current_page - 1
158
+ prev_page = nil if prev_page < 1
159
+
160
+ min_page = current_page - side_pages
161
+ min_page = total_pages - (side_pages * 2) if (current_page + side_pages) > total_pages
162
+ min_page = 1 if min_page < 1
163
+
164
+ max_page = current_page + side_pages
165
+ max_page = side_pages * 2 if current_page < side_pages
166
+ max_page = total_pages if max_page > total_pages
167
+
168
+ offset = (current_page - 1) * items_per_page
169
+ offset = 0 if offset < 0
170
+
171
+ counter = {a: offset + 1, z: offset + items_per_page, total: total_items}
172
+ counter[:a] = offset if counter[:a] > total_items
173
+ counter[:z] = total_items if counter[:z] > total_items
174
+
175
+ [offset, {
176
+ current_page: current_page,
177
+ total_pages: total_pages,
178
+ next_page: next_page,
179
+ prev_page: prev_page,
180
+ pages_list: (min_page .. max_page).to_a,
181
+ counter: counter
182
+ }]
183
+ end
184
+
185
+ end
12
186
  end
13
187
  end
14
188
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crudle
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
  - Slee Woo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-25 00:00:00.000000000 Z
11
+ date: 2016-05-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Building blocks for opinionated CRUD interfaces
14
14
  email:
@@ -22,8 +22,7 @@ files:
22
22
  - Rakefile
23
23
  - crudle.gemspec
24
24
  - lib/crudle.rb
25
- - lib/crudle/controller_setup.rb
26
- - lib/crudle/instance_methods.rb
25
+ - lib/crudle/version.rb
27
26
  homepage: https://github.com/crudle/ruby-server
28
27
  licenses:
29
28
  - MIT
@@ -47,5 +46,5 @@ rubyforge_project:
47
46
  rubygems_version: 2.5.1
48
47
  signing_key:
49
48
  specification_version: 4
50
- summary: '["crudle-0.0.1", "Building blocks for opinionated CRUD interfaces"]'
49
+ summary: '["crudle-0.0.3", "Building blocks for opinionated CRUD interfaces"]'
51
50
  test_files: []
@@ -1,44 +0,0 @@
1
- module Crudle
2
- ControllerSetup = -> do
3
- include Crudle::InstanceMethods
4
-
5
- def list params = {}
6
- dataset = filtered_dataset(params)
7
- offset, pager = pager(params, dataset.count, items_per_page, side_pages)
8
- {
9
- items: fetch_items(dataset, offset, params[:id]).map {|i| list_serializer(i)},
10
- pager: pager
11
- }
12
- end
13
-
14
-
15
- def load id
16
- editor_serializer(find_item(id))
17
- end
18
-
19
-
20
- def create
21
- model.create.id
22
- end
23
-
24
-
25
- def save id, data
26
- item = find_item(id)
27
- data.each_pair {|k,v| item[k] = v}
28
- item.valid? || halt(400, error: item.errors.each_with_object([]) {|e,o| o << e.join(': ')})
29
- item.save
30
- list_serializer(item)
31
- end
32
-
33
-
34
- def delete id
35
- find_item(id).destroy
36
- end
37
-
38
-
39
- def delete_selected params, selected
40
- model.where(id: selected).map(&:destroy)
41
- list(params)
42
- end
43
- end
44
- end
@@ -1,115 +0,0 @@
1
- module Crudle
2
- module InstanceMethods
3
-
4
- private
5
- def find_item id
6
- model.find_by(id: id) || halt(400, 'No item found with ID #%s' % escape_html(id.to_s))
7
- end
8
-
9
-
10
- def model
11
- raise('Model not set. Please define a #model method that returns your model')
12
- end
13
-
14
-
15
- def items_per_page
16
- 25
17
- end
18
-
19
-
20
- def side_pages
21
- 4
22
- end
23
-
24
-
25
- def order
26
- {id: :desc}
27
- end
28
-
29
-
30
- def list_serializer item
31
- serializer(item)
32
- end
33
-
34
-
35
- def editor_serializer item
36
- serializer(item)
37
- end
38
-
39
-
40
- def serializer item
41
- item
42
- end
43
-
44
-
45
- def filter params, dataset
46
- return if params[:q].nil? || params[:q].empty?
47
- if id = params[:q].scan(/id=(\d+)/).flatten[0]
48
- return dataset.where(id: id)
49
- end
50
- dataset.where(Crudle::NAME_QUERY, Crudle::NAME_QUERY_FORMAT % params[:q])
51
- end
52
-
53
-
54
- def filtered_dataset params
55
- params.inject(filter(params, model) || model) do |dataset,(k,v)|
56
- filter_method = Crudle::FILTER_METHOD_FORMAT % k
57
- if respond_to?(filter_method)
58
- send(filter_method, params, dataset) || dataset
59
- else
60
- dataset
61
- end
62
- end
63
- end
64
-
65
-
66
- def fetch_items dataset, offset, id = nil
67
- items = model.unscoped do
68
- dataset.order(order).offset(offset).limit(items_per_page).to_a
69
- end
70
- if id && item = model.find_by(id: id)
71
- items.delete_if {|i| i.id == item.id}
72
- items.unshift(item)
73
- end
74
- items
75
- end
76
-
77
-
78
- def pager params, total_items, items_per_page, side_pages
79
- total_pages = (total_items.to_f / items_per_page.to_f).ceil
80
-
81
- current_page = params[:p].to_i
82
- current_page = 1 if current_page < 1
83
- current_page = total_pages if current_page > total_pages
84
-
85
- next_page = current_page + 1
86
- next_page = nil if next_page > total_pages
87
- prev_page = current_page - 1
88
- prev_page = nil if prev_page < 1
89
-
90
- min_page = current_page - side_pages
91
- min_page = total_pages - (side_pages * 2) if (current_page + side_pages) > total_pages
92
- min_page = 1 if min_page < 1
93
-
94
- max_page = current_page + side_pages
95
- max_page = side_pages * 2 if current_page < side_pages
96
- max_page = total_pages if max_page > total_pages
97
-
98
- offset = (current_page - 1) * items_per_page
99
- offset = 0 if offset < 0
100
-
101
- counter = {a: offset + 1, z: offset + items_per_page, total: total_items}
102
- counter[:a] = offset if counter[:a] > total_items
103
- counter[:z] = total_items if counter[:z] > total_items
104
-
105
- [offset, {
106
- current_page: current_page,
107
- total_pages: total_pages,
108
- next_page: next_page,
109
- prev_page: prev_page,
110
- pages_list: (min_page .. max_page).to_a,
111
- counter: counter
112
- }]
113
- end
114
- end
115
- end