azahara_schema 0.1.1 → 0.1.2

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: 8fdc281f1678559822f32f72ae8dc63a8ae3cb0c
4
- data.tar.gz: 94a238cae3c82df5573c651a81d9b5d0ea3e78f1
3
+ metadata.gz: 97da3d5692c281b23103b2f29bb7d000e9a9a47d
4
+ data.tar.gz: 6650308c13f3b416394d6899067768300f6a022a
5
5
  SHA512:
6
- metadata.gz: 0b5c33963f165442367dcb014f17b5d287094cda153da630815eae239bcf6fc166f40591339be34932d3dc4fba02154a876ed672ab9d78ba369f08f4727fda33
7
- data.tar.gz: c3bb384bcde4c9a27881c86b39ca42ccc120b2f3d8b80b3470754e4f8bbaea8f82f01d8ddcd22b9790a1a54c79bea3cd651ba09b6b1b57e4667bbdf421afa838
6
+ metadata.gz: b5a10fdff991b788f56fe2ffe2f3c72f65d3f8feea779dc5766706a31bc69130cbc2154014b78cd0088713400ae09f2d81b72d2130066f468ddc4432716a57e6
7
+ data.tar.gz: 7a1f06279dfeed7351b9d10bbaeb9b5c3f0196c3d2fa6a26f29a4e1951599b56744b433e8e74266c81bd5f30b0e37bee7a06a1bac402b322fdbcab04e6b85247
data/README.md CHANGED
@@ -1,8 +1,14 @@
1
+ [![pipeline status](https://git.servis.justice.cz/libraries/azahara_schema/badges/master/pipeline.svg)](https://git.servis.justice.cz/libraries/azahara_schema/commits/master)
2
+
1
3
  # AzaharaSchema
2
4
 
5
+ Azahara schema helps you to work with your model view.
6
+ Its basic usage is for your index method. But can be used even as a master-detail component.
3
7
 
4
- ## Usage
5
- How to use my plugin.
8
+ * It defines filters for you.
9
+ * It renders json responses for you
10
+ * Defines easy api over you model
11
+ * Lets you define your own outputs and manages them for you.
6
12
 
7
13
  ## Installation
8
14
  Add this line to your application's Gemfile:
@@ -21,6 +27,52 @@ Or install it yourself as:
21
27
  $ gem install azahara_schema
22
28
  ```
23
29
 
30
+ ## Usage
31
+ Default usage is only for Rails.
32
+
33
+ You basically need just this:
34
+ ```ruby
35
+ @schema = AzaharaSchema::Schema.new(Model)
36
+ ```
37
+
38
+ But you can ovewrite the default behaviour. Just inherit from AzaharaSchema::ModelSchema
39
+ ```ruby
40
+ class UserSchema < AzaharaSchema::ModelSchema
41
+ end
42
+
43
+ UserSchema.new
44
+ ```
45
+
46
+ Azahara takes the name of model before Schema and uses it for looking for model class.
47
+ Alternatively you can overwrite method ```model``` and let it return class for your model.
48
+
49
+
50
+ ### Defining enabled/disabled filters
51
+
52
+ In your inherited class you can globally set enabled filter - for azahara not to populate unwanted parameters of your model.
53
+ ```ruby
54
+ self.enabled_filters 'attr_name1', 'attr_name2'
55
+ ```
56
+ Second option is defining those on instance - it is preferable solution and is more straight forward.
57
+ Here you can even solve the permissions.
58
+ ```ruby
59
+ def enabled_filters
60
+ filters = ['attr_name1', 'attr_name2']
61
+ filters << 'attr_name3' if User.current.admin?
62
+ filters
63
+ end
64
+ ```
65
+ Alternative for it is just black list attributes you do not want to have as filters.
66
+ ```ruby
67
+ def disabled_filters
68
+ to_disable = []
69
+ to_disable << ['attr_name3'] if !User.current.admin?
70
+ to_disable
71
+ end
72
+ ```
73
+ Or you can use all those methods together.
74
+
75
+
24
76
  ## Contributing
25
77
  Contribution directions go here.
26
78
 
@@ -66,6 +66,8 @@ module AzaharaSchema
66
66
  scope.where( arel_field.gteq(values) )
67
67
  when '<='
68
68
  scope.where( arel_field.lteq(values) )
69
+ else
70
+ throw 'Unknown operator ' + operator.to_s
69
71
  end
70
72
  end
71
73
 
@@ -59,6 +59,10 @@ module AzaharaSchema
59
59
  add 'float'
60
60
  end
61
61
 
62
+ class DecimalFormat < FloatFormat
63
+ add 'decimal'
64
+ end
65
+
62
66
  class BooleanFormat < Base
63
67
  add 'boolean'
64
68
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support' #Hash.slice
2
+
1
3
  module AzaharaSchema
2
4
  class Schema
3
5
 
@@ -35,6 +37,11 @@ module AzaharaSchema
35
37
  @column_names = attributes[:columns]
36
38
  end
37
39
 
40
+ def column_names=(values)
41
+ @column_names = values
42
+ @columns = nil
43
+ end
44
+
38
45
  def column_names
39
46
  @column_names ||= default_columns
40
47
  end
@@ -116,15 +123,22 @@ module AzaharaSchema
116
123
  end
117
124
 
118
125
  def enabled_filters
119
- if self.class.enabled_filters.any?
120
- self.class.enabled_filters.collect{|f_name| available_attributes.detect{|attr| attr.name == f_name } }.compact
121
- else
122
- available_attributes
123
- end
126
+
127
+ end
128
+
129
+ def disabled_filters
130
+ []
131
+ end
132
+
133
+ def enabled_filter_names
134
+ names = self.class.enabled_filters if self.class.enabled_filters.any?
135
+ names ||= available_attributes_hash.keys
136
+ names &= enabled_filters if enabled_filters
137
+ names -= disabled_filters
124
138
  end
125
139
 
126
140
  def available_filters
127
- @available_filters ||= enabled_filters.select{|att| att.filter? }.collect{|att| [att.filter_name, att] }.to_h
141
+ @available_filters ||= available_attributes_hash.slice(*enabled_filter_names)
128
142
  end
129
143
 
130
144
  def available_associations
@@ -185,6 +199,9 @@ module AzaharaSchema
185
199
  filter_params = params[:f].permit(available_filters.keys).to_h
186
200
  filter_params.each{|name, short_filter| add_short_filter(name, short_filter) }
187
201
  end
202
+ if params[:c].is_a?(Array)
203
+ self.column_names = params[:c].to_a
204
+ end
188
205
  if params[:sort]
189
206
  @sort = nil
190
207
  params[:sort].each do |k, sort|
@@ -199,6 +216,7 @@ module AzaharaSchema
199
216
  filters.each do |fname, attrs|
200
217
  params[:f][fname] = "#{attrs[:o]}|#{attrs[:v]}"
201
218
  end
219
+ params[:c] = column_names
202
220
  params
203
221
  end
204
222
 
@@ -1,3 +1,3 @@
1
1
  module AzaharaSchema
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azahara_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondřej Ezr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubyforge_project:
98
- rubygems_version: 2.6.13
98
+ rubygems_version: 2.6.14
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: Gem to support developement of rails application with schema over an entity