azahara_schema 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
 - data/README.md +54 -2
 - data/lib/azahara_schema/attribute.rb +2 -0
 - data/lib/azahara_schema/field_format.rb +4 -0
 - data/lib/azahara_schema/schema.rb +24 -6
 - data/lib/azahara_schema/version.rb +1 -1
 - metadata +3 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 97da3d5692c281b23103b2f29bb7d000e9a9a47d
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 6650308c13f3b416394d6899067768300f6a022a
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: b5a10fdff991b788f56fe2ffe2f3c72f65d3f8feea779dc5766706a31bc69130cbc2154014b78cd0088713400ae09f2d81b72d2130066f468ddc4432716a57e6
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 7a1f06279dfeed7351b9d10bbaeb9b5c3f0196c3d2fa6a26f29a4e1951599b56744b433e8e74266c81bd5f30b0e37bee7a06a1bac402b322fdbcab04e6b85247
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,8 +1,14 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            [](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 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
      
 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 
     | 
    
         | 
| 
         @@ -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 
     | 
    
         
            -
             
     | 
| 
       120 
     | 
    
         
            -
             
     | 
| 
       121 
     | 
    
         
            -
             
     | 
| 
       122 
     | 
    
         
            -
             
     | 
| 
       123 
     | 
    
         
            -
                   
     | 
| 
      
 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 ||=  
     | 
| 
      
 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 
     | 
    
         | 
    
        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. 
     | 
| 
      
 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- 
     | 
| 
      
 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. 
     | 
| 
      
 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
         
     |