rails-datatable 0.1.0 → 0.1.1
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/.gitignore +2 -0
 - data/README.md +2 -2
 - data/lib/rails/datatable/version.rb +1 -1
 - metadata +2 -6
 - data/app/assets/javascripts/application.js +0 -14
 - data/app/datatables/datatable.rb +0 -81
 - data/app/datatables/users_datatable.rb +0 -14
 - data/app/views/users/_user.html.erb +0 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: f71cde90643548ca257324d3a30e9c7a136e358c17279a59feb13a4e6a8c1cf2
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 2241d5b9963030510f7715f70fae37c9f089d95b29426ae9c5a91fba79fdc5be
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 1268b49b0e96905018eb8d9ccc5e71014a37c10bac73a0a3d25ff04bce57c845c9b2c27100e956d5e5523f6a7cd317745b59724bcc96893f8b83e3bd1160e95f
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 46cb758453a272f7fd99ae1c7cdadd05691c458f2ad6ecc6668a535f884a437a8194366f798fff7f7c03cb23cd19376d5d1b3716cb303505afb644fdb940812a
         
     | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | 
         @@ -19,12 +19,12 @@ Or install it yourself as: 
     | 
|
| 
       19 
19 
     | 
    
         
             
            ## Usage
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
21 
     | 
    
         
             
            ### 1. Install RailsDatatable
         
     | 
| 
       22 
     | 
    
         
            -
            	$ rails g rails 
     | 
| 
      
 22 
     | 
    
         
            +
            	$ rails g rails:datatable:install
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
24 
     | 
    
         
             
            ### 2. Generate resources
         
     | 
| 
       25 
25 
     | 
    
         
             
            #### 2.1 Generate databtable resources
         
     | 
| 
       26 
26 
     | 
    
         | 
| 
       27 
     | 
    
         
            -
            	$ rails g rails 
     | 
| 
      
 27 
     | 
    
         
            +
            	$ rails g rails:datatable:resource User
         
     | 
| 
       28 
28 
     | 
    
         | 
| 
       29 
29 
     | 
    
         
             
            #### 2.2 Implement json method in your controller
         
     | 
| 
       30 
30 
     | 
    
         
             
            ```ruby
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: rails-datatable
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.1
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Jacky
         
     | 
| 
         @@ -110,10 +110,6 @@ files: 
     | 
|
| 
       110 
110 
     | 
    
         
             
            - LICENSE.txt
         
     | 
| 
       111 
111 
     | 
    
         
             
            - README.md
         
     | 
| 
       112 
112 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       113 
     | 
    
         
            -
            - app/assets/javascripts/application.js
         
     | 
| 
       114 
     | 
    
         
            -
            - app/datatables/datatable.rb
         
     | 
| 
       115 
     | 
    
         
            -
            - app/datatables/users_datatable.rb
         
     | 
| 
       116 
     | 
    
         
            -
            - app/views/users/_user.html.erb
         
     | 
| 
       117 
113 
     | 
    
         
             
            - bin/console
         
     | 
| 
       118 
114 
     | 
    
         
             
            - bin/setup
         
     | 
| 
       119 
115 
     | 
    
         
             
            - lib/generators/rails/datatable/install_generator.rb
         
     | 
| 
         @@ -148,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       148 
144 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       149 
145 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       150 
146 
     | 
    
         
             
            requirements: []
         
     | 
| 
       151 
     | 
    
         
            -
            rubygems_version: 3. 
     | 
| 
      
 147 
     | 
    
         
            +
            rubygems_version: 3.1.4
         
     | 
| 
       152 
148 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       153 
149 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       154 
150 
     | 
    
         
             
            summary: Simple datatable integration for Rails app
         
     | 
| 
         @@ -1,14 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            // This is a manifest file that'll be compiled into application.js, which will include all the files
         
     | 
| 
       2 
     | 
    
         
            -
            // listed below.
         
     | 
| 
       3 
     | 
    
         
            -
            //
         
     | 
| 
       4 
     | 
    
         
            -
            // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
         
     | 
| 
       5 
     | 
    
         
            -
            // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
         
     | 
| 
       6 
     | 
    
         
            -
            //
         
     | 
| 
       7 
     | 
    
         
            -
            // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
         
     | 
| 
       8 
     | 
    
         
            -
            // compiled file.
         
     | 
| 
       9 
     | 
    
         
            -
            //
         
     | 
| 
       10 
     | 
    
         
            -
            // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
         
     | 
| 
       11 
     | 
    
         
            -
            // about supported directives.
         
     | 
| 
       12 
     | 
    
         
            -
            //
         
     | 
| 
       13 
     | 
    
         
            -
            //= require jquery
         
     | 
| 
       14 
     | 
    
         
            -
            //= require jquery.dataTables.min
         
     | 
    
        data/app/datatables/datatable.rb
    DELETED
    
    | 
         @@ -1,81 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            class Datatable
         
     | 
| 
       2 
     | 
    
         
            -
              include Rails.application.routes.url_helpers
         
     | 
| 
       3 
     | 
    
         
            -
             
     | 
| 
       4 
     | 
    
         
            -
              delegate :params, :raw, :h, :link_to,
         
     | 
| 
       5 
     | 
    
         
            -
                :number_to_currency, :text_field_tag,
         
     | 
| 
       6 
     | 
    
         
            -
                :check_box_tag, :current_user, :image_tag, :content_tag,
         
     | 
| 
       7 
     | 
    
         
            -
                :check_box_tag, :select_tag, :options_from_collection_for_select,:number_with_precision,
         
     | 
| 
       8 
     | 
    
         
            -
                :hidden_field_tag, :request, :render, to: :@view
         
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
              DEFAULT = {
         
     | 
| 
       11 
     | 
    
         
            -
                per_page: 10,
         
     | 
| 
       12 
     | 
    
         
            -
                truncate_length: 50,
         
     | 
| 
       13 
     | 
    
         
            -
                na: 'N/A'
         
     | 
| 
       14 
     | 
    
         
            -
              }
         
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
              def initialize(view, klass, columns_for_search: [], columns_for_sort: [], options: {})
         
     | 
| 
       17 
     | 
    
         
            -
                @view = view
         
     | 
| 
       18 
     | 
    
         
            -
                @klass = klass
         
     | 
| 
       19 
     | 
    
         
            -
                @columns_for_search = columns_for_search
         
     | 
| 
       20 
     | 
    
         
            -
                @columns_for_sort = columns_for_sort
         
     | 
| 
       21 
     | 
    
         
            -
              end
         
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
              def as_json(options = {})
         
     | 
| 
       24 
     | 
    
         
            -
                {
         
     | 
| 
       25 
     | 
    
         
            -
                  sEcho: params[:sEcho].to_i,
         
     | 
| 
       26 
     | 
    
         
            -
                  iTotalRecords: @klass.count,
         
     | 
| 
       27 
     | 
    
         
            -
                  iTotalDisplayRecords: items.total_count,
         
     | 
| 
       28 
     | 
    
         
            -
                  data: data
         
     | 
| 
       29 
     | 
    
         
            -
                }
         
     | 
| 
       30 
     | 
    
         
            -
              end
         
     | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
       32 
     | 
    
         
            -
              protected
         
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
              def data
         
     | 
| 
       35 
     | 
    
         
            -
                raise NotImplementedError # Override this method in subclass
         
     | 
| 
       36 
     | 
    
         
            -
              end
         
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
              def items
         
     | 
| 
       39 
     | 
    
         
            -
                @items ||= fetch_items
         
     | 
| 
       40 
     | 
    
         
            -
              end
         
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
              def fetch_items
         
     | 
| 
       43 
     | 
    
         
            -
                items = if sort_column == '-' 
         
     | 
| 
       44 
     | 
    
         
            -
                  @klass.unscope(:order).order(:created_at => :desc)
         
     | 
| 
       45 
     | 
    
         
            -
                else
         
     | 
| 
       46 
     | 
    
         
            -
                  null_order = sort_direction == 'asc' ? 'FIRST' : 'LAST'
         
     | 
| 
       47 
     | 
    
         
            -
                  @klass.unscope(:order).order("#{sort_column} #{sort_direction} NULLS #{null_order}")
         
     | 
| 
       48 
     | 
    
         
            -
                end
         
     | 
| 
       49 
     | 
    
         
            -
                
         
     | 
| 
       50 
     | 
    
         
            -
                items = items.page(page).per(per_page)
         
     | 
| 
       51 
     | 
    
         
            -
                
         
     | 
| 
       52 
     | 
    
         
            -
                if params[:search].present?
         
     | 
| 
       53 
     | 
    
         
            -
                  search_str = @columns_for_search.map{|field| "upper(cast(#{field} as text)) like upper(:search)"}.join ' or '
         
     | 
| 
       54 
     | 
    
         
            -
                  items = items.where(search_str, search: "%#{params[:search][:value].strip}%")
         
     | 
| 
       55 
     | 
    
         
            -
                end
         
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
                if params[:sSearch].present?
         
     | 
| 
       58 
     | 
    
         
            -
                  search_str = @columns_for_search.map{|field| "upper(cast(#{field} as text)) like upper(:search)"}.join ' or '
         
     | 
| 
       59 
     | 
    
         
            -
                  items = items.where(search_str, search: "%#{params[:sSearch].strip}%")
         
     | 
| 
       60 
     | 
    
         
            -
                end
         
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
                items
         
     | 
| 
       63 
     | 
    
         
            -
              end
         
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
              def page
         
     | 
| 
       66 
     | 
    
         
            -
                (params['start'].to_i / per_page) + 1 rescue 1
         
     | 
| 
       67 
     | 
    
         
            -
              end
         
     | 
| 
       68 
     | 
    
         
            -
             
     | 
| 
       69 
     | 
    
         
            -
              def per_page
         
     | 
| 
       70 
     | 
    
         
            -
                params['length'].to_i rescue 10
         
     | 
| 
       71 
     | 
    
         
            -
              end
         
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
       73 
     | 
    
         
            -
              def sort_column
         
     | 
| 
       74 
     | 
    
         
            -
                sort_index = params['order']['0']['column'].to_i rescue 0
         
     | 
| 
       75 
     | 
    
         
            -
                @columns_for_sort[sort_index]
         
     | 
| 
       76 
     | 
    
         
            -
              end
         
     | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
              def sort_direction
         
     | 
| 
       79 
     | 
    
         
            -
                params['order']['0']['dir'] rescue 'asc'
         
     | 
| 
       80 
     | 
    
         
            -
              end
         
     | 
| 
       81 
     | 
    
         
            -
            end
         
     | 
| 
         @@ -1,14 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            class UsersDatatable < Datatable
         
     | 
| 
       2 
     | 
    
         
            -
              def initialize(view, klass)
         
     | 
| 
       3 
     | 
    
         
            -
                super(view, klass, columns_for_search: %w[], columns_for_sort:%w[- - - - ])
         
     | 
| 
       4 
     | 
    
         
            -
              end
         
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
              def data
         
     | 
| 
       7 
     | 
    
         
            -
                @items.each_with_index.map do |item, index|
         
     | 
| 
       8 
     | 
    
         
            -
                  render(
         
     | 
| 
       9 
     | 
    
         
            -
                    partial: 'users/user',
         
     | 
| 
       10 
     | 
    
         
            -
                    formats: [:html],
         
     | 
| 
       11 
     | 
    
         
            -
                    locals:  { item: item, index: index}).split('</td>').map {|row| row.gsub('<td>', '')}
         
     | 
| 
       12 
     | 
    
         
            -
                end
         
     | 
| 
       13 
     | 
    
         
            -
              end
         
     | 
| 
       14 
     | 
    
         
            -
            end
         
     |