rice_cooker 0.1.2 → 0.1.3
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 +1 -0
- data/Gemfile +0 -12
- data/Gemfile.lock +4 -23
- data/lib/rice_cooker/class_methods.rb +57 -0
- data/lib/rice_cooker/filter.rb +6 -8
- data/lib/rice_cooker/helpers.rb +5 -50
- data/lib/rice_cooker/range.rb +1 -3
- data/lib/rice_cooker/sort.rb +8 -9
- data/lib/rice_cooker/version.rb +1 -1
- data/lib/rice_cooker.rb +20 -8
- data/rice_cooker.gemspec +0 -3
- data/spec/range/range_spec.rb +23 -31
- metadata +3 -44
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: febeec59cbb060cf35bb66891a2c3017876f6263
         | 
| 4 | 
            +
              data.tar.gz: 80ec86ea16fc1496809c648789e937e7592332a0
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 0b54debc8ddc3c88ae32a8b2eff042e4c1fa14bd73b6ccbc9ac32b0e2bb8c95b26db833186eadee1ac80639dad42037b69940cc4a7e4121f0c58272941525897
         | 
| 7 | 
            +
              data.tar.gz: 3d46fbf0f23c24ef4afa1c01e72612a843f8b28cdc10db72ac5d8d4d39558cb9f2b7c2090d0d1ca952e02b97adb37a28edfedf159fdc90b5247a9eb620b72e2a
         | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/Gemfile
    CHANGED
    
    | @@ -1,14 +1,2 @@ | |
| 1 1 | 
             
            source 'https://rubygems.org'
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            # Declare your gem's dependencies in rice_cooker.gemspec.
         | 
| 4 | 
            -
            # Bundler will treat runtime dependencies like base dependencies, and
         | 
| 5 | 
            -
            # development dependencies will be added by default to the :development group.
         | 
| 6 2 | 
             
            gemspec
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            # Declare any dependencies that are still in development here instead of in
         | 
| 9 | 
            -
            # your gemspec. These might include edge Rails or gems from your path or
         | 
| 10 | 
            -
            # Git. Remember to move these dependencies to your gemspec before releasing
         | 
| 11 | 
            -
            # your gem to rubygems.org.
         | 
| 12 | 
            -
             | 
| 13 | 
            -
            # To use a debugger
         | 
| 14 | 
            -
            # gem 'byebug', group: [:development, :test]
         | 
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,11 +1,11 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                rice_cooker (0.1. | 
| 5 | 
            -
                  actionpack ( | 
| 4 | 
            +
                rice_cooker (0.1.3)
         | 
| 5 | 
            +
                  actionpack (>= 4.2.0, < 5.1)
         | 
| 6 6 | 
             
                  has_scope (~> 0.7.0, >= 0.6.0)
         | 
| 7 | 
            -
                  rails ( | 
| 8 | 
            -
                  railties ( | 
| 7 | 
            +
                  rails (>= 4.2.0, < 5.1)
         | 
| 8 | 
            +
                  railties (>= 4.2.0, < 5.1)
         | 
| 9 9 |  | 
| 10 10 | 
             
            GEM
         | 
| 11 11 | 
             
              remote: https://rubygems.org/
         | 
| @@ -49,21 +49,10 @@ GEM | |
| 49 49 | 
             
                  tzinfo (~> 1.1)
         | 
| 50 50 | 
             
                arel (7.0.0)
         | 
| 51 51 | 
             
                ast (2.3.0)
         | 
| 52 | 
            -
                benchmark-ips (2.6.1)
         | 
| 53 52 | 
             
                builder (3.2.2)
         | 
| 54 53 | 
             
                coderay (1.1.1)
         | 
| 55 54 | 
             
                concurrent-ruby (1.0.2)
         | 
| 56 55 | 
             
                database_cleaner (1.5.3)
         | 
| 57 | 
            -
                derailed (0.1.0)
         | 
| 58 | 
            -
                  derailed_benchmarks
         | 
| 59 | 
            -
                derailed_benchmarks (1.3.1)
         | 
| 60 | 
            -
                  benchmark-ips (~> 2)
         | 
| 61 | 
            -
                  get_process_mem (~> 0)
         | 
| 62 | 
            -
                  heapy (~> 0)
         | 
| 63 | 
            -
                  memory_profiler (~> 0)
         | 
| 64 | 
            -
                  rack (>= 1)
         | 
| 65 | 
            -
                  rake (> 10, < 12)
         | 
| 66 | 
            -
                  thor (~> 0.19)
         | 
| 67 56 | 
             
                diff-lcs (1.2.5)
         | 
| 68 57 | 
             
                docile (1.1.5)
         | 
| 69 58 | 
             
                erubis (2.7.0)
         | 
| @@ -74,20 +63,17 @@ GEM | |
| 74 63 | 
             
                  railties (>= 3.0.0)
         | 
| 75 64 | 
             
                faker (1.6.1)
         | 
| 76 65 | 
             
                  i18n (~> 0.5)
         | 
| 77 | 
            -
                get_process_mem (0.2.1)
         | 
| 78 66 | 
             
                globalid (0.3.6)
         | 
| 79 67 | 
             
                  activesupport (>= 4.1.0)
         | 
| 80 68 | 
             
                has_scope (0.7.0)
         | 
| 81 69 | 
             
                  actionpack (>= 4.1, < 5.1)
         | 
| 82 70 | 
             
                  activesupport (>= 4.1, < 5.1)
         | 
| 83 | 
            -
                heapy (0.1.2)
         | 
| 84 71 | 
             
                i18n (0.7.0)
         | 
| 85 72 | 
             
                json (1.8.3)
         | 
| 86 73 | 
             
                loofah (2.0.3)
         | 
| 87 74 | 
             
                  nokogiri (>= 1.5.9)
         | 
| 88 75 | 
             
                mail (2.6.4)
         | 
| 89 76 | 
             
                  mime-types (>= 1.16, < 4)
         | 
| 90 | 
            -
                memory_profiler (0.9.6)
         | 
| 91 77 | 
             
                metaclass (0.0.4)
         | 
| 92 78 | 
             
                method_source (0.8.2)
         | 
| 93 79 | 
             
                mime-types (3.1)
         | 
| @@ -130,7 +116,6 @@ GEM | |
| 130 116 | 
             
                  nokogiri (~> 1.6.0)
         | 
| 131 117 | 
             
                rails-html-sanitizer (1.0.3)
         | 
| 132 118 | 
             
                  loofah (~> 2.0)
         | 
| 133 | 
            -
                rails-perftest (0.0.6)
         | 
| 134 119 | 
             
                railties (5.0.0)
         | 
| 135 120 | 
             
                  actionpack (= 5.0.0)
         | 
| 136 121 | 
             
                  activesupport (= 5.0.0)
         | 
| @@ -186,7 +171,6 @@ GEM | |
| 186 171 | 
             
                  activesupport (>= 4.0)
         | 
| 187 172 | 
             
                  sprockets (>= 3.0.0)
         | 
| 188 173 | 
             
                sqlite3 (1.3.11)
         | 
| 189 | 
            -
                stackprof (0.2.9)
         | 
| 190 174 | 
             
                test-unit (3.1.3)
         | 
| 191 175 | 
             
                  power_assert
         | 
| 192 176 | 
             
                thor (0.19.1)
         | 
| @@ -203,12 +187,10 @@ PLATFORMS | |
| 203 187 |  | 
| 204 188 | 
             
            DEPENDENCIES
         | 
| 205 189 | 
             
              database_cleaner
         | 
| 206 | 
            -
              derailed
         | 
| 207 190 | 
             
              factory_girl_rails (~> 4.0)
         | 
| 208 191 | 
             
              faker (= 1.6.1)
         | 
| 209 192 | 
             
              mocha (= 1.1.0)
         | 
| 210 193 | 
             
              pry
         | 
| 211 | 
            -
              rails-perftest (= 0.0.6)
         | 
| 212 194 | 
             
              rice_cooker!
         | 
| 213 195 | 
             
              rspec
         | 
| 214 196 | 
             
              rspec-activemodel-mocks
         | 
| @@ -217,7 +199,6 @@ DEPENDENCIES | |
| 217 199 | 
             
              ruby-prof (= 0.15.8)
         | 
| 218 200 | 
             
              simplecov (= 0.11.1)
         | 
| 219 201 | 
             
              sqlite3
         | 
| 220 | 
            -
              stackprof
         | 
| 221 202 | 
             
              test-unit (= 3.1.3)
         | 
| 222 203 |  | 
| 223 204 | 
             
            BUNDLED WITH
         | 
| @@ -0,0 +1,57 @@ | |
| 1 | 
            +
            require 'active_support'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module RiceCooker
         | 
| 4 | 
            +
              module ClassMethods
         | 
| 5 | 
            +
                protected
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def initialize_model_class!
         | 
| 8 | 
            +
                  # First priority is the namespaced model, e.g. User::Group
         | 
| 9 | 
            +
                  self.resource_model ||= begin
         | 
| 10 | 
            +
                    namespaced_class = name.sub(/Controller$/, '').singularize
         | 
| 11 | 
            +
                    namespaced_class.constantize
         | 
| 12 | 
            +
                  rescue NameError
         | 
| 13 | 
            +
                    nil
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  # Second priority is the top namespace model, e.g. EngineName::Article for EngineName::Admin::ArticlesController
         | 
| 17 | 
            +
                  self.resource_model ||= begin
         | 
| 18 | 
            +
                    namespaced_classes = name.sub(/Controller$/, '').split('::')
         | 
| 19 | 
            +
                    namespaced_class = [namespaced_classes.first, namespaced_classes.last].join('::').singularize
         | 
| 20 | 
            +
                    namespaced_class.constantize
         | 
| 21 | 
            +
                  rescue NameError
         | 
| 22 | 
            +
                    nil
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  # Third priority the camelcased c, i.e. UserGroup
         | 
| 26 | 
            +
                  self.resource_model ||= begin
         | 
| 27 | 
            +
                    camelcased_class = name.sub(/Controller$/, '').gsub('::', '').singularize
         | 
| 28 | 
            +
                    camelcased_class.constantize
         | 
| 29 | 
            +
                  rescue NameError
         | 
| 30 | 
            +
                    nil
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                  # Otherwise use the Group class, or fail
         | 
| 34 | 
            +
                  self.resource_model ||= begin
         | 
| 35 | 
            +
                    class_name = controller_name.classify
         | 
| 36 | 
            +
                    class_name.constantize
         | 
| 37 | 
            +
                  rescue NameError => e
         | 
| 38 | 
            +
                    raise unless e.message.include?(class_name)
         | 
| 39 | 
            +
                    nil
         | 
| 40 | 
            +
                  end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                  # We prevent for creating a resource wich is not a model
         | 
| 43 | 
            +
                  if self.resource_model
         | 
| 44 | 
            +
                    begin
         | 
| 45 | 
            +
                      self.resource_model = nil unless self.resource_model < ActiveRecord::Base
         | 
| 46 | 
            +
                    rescue
         | 
| 47 | 
            +
                      nil
         | 
| 48 | 
            +
                    end
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                def inherited(base) #:nodoc:
         | 
| 53 | 
            +
                  super(base)
         | 
| 54 | 
            +
                  base.send :initialize_model_class!
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
            end
         | 
    
        data/lib/rice_cooker/filter.rb
    CHANGED
    
    | @@ -13,16 +13,14 @@ module RiceCooker | |
| 13 13 | 
             
                    cattr_accessor :filtering_keys
         | 
| 14 14 | 
             
                    cattr_accessor :custom_filters
         | 
| 15 15 |  | 
| 16 | 
            -
                    resource_class ||= controller_resource_class(self)# unless respond_to?(:resource_class)
         | 
| 17 | 
            -
             | 
| 18 16 | 
             
                    # On normalize tout ca
         | 
| 19 17 | 
             
                    additional_filtering_params = format_additional_param(additional_filtering_params, 'filtering')
         | 
| 20 18 |  | 
| 21 19 | 
             
                    # On recupere tous les filtres autorisés
         | 
| 22 | 
            -
                    allowed_keys = (filterable_fields_for( | 
| 20 | 
            +
                    allowed_keys = (filterable_fields_for(resource_model) + additional_filtering_params.keys)
         | 
| 23 21 |  | 
| 24 22 | 
             
                    # On fait une sorte de *register_bool_filter* sur tous les champs *_at
         | 
| 25 | 
            -
                    additional = (filterable_fields_for( | 
| 23 | 
            +
                    additional = (filterable_fields_for(resource_model) - [:created_at, :updated_at])
         | 
| 26 24 | 
             
                                 .select { |e| e =~ /_at$/ }
         | 
| 27 25 | 
             
                                 .select { |e| additional_filtering_params[e.to_s.gsub(/_at$/, '')].nil? }
         | 
| 28 26 |  | 
| @@ -30,18 +28,18 @@ module RiceCooker | |
| 30 28 | 
             
                      name = fi.to_s.gsub(/_at$/, '')
         | 
| 31 29 |  | 
| 32 30 | 
             
                      if fi.to_sym == :begin_at
         | 
| 33 | 
            -
                        db_field = "#{ | 
| 31 | 
            +
                        db_field = "#{resource_model.quoted_table_name}.\"#{fi}\""
         | 
| 34 32 | 
             
                        additional_filtering_params[:future] = {
         | 
| 35 33 | 
             
                          proc: -> (value) { value.first == 'true' ? where("#{db_field} >= ?", Time.zone.now) : where("#{db_field} < ?", Time.zone.now) },
         | 
| 36 34 | 
             
                          all: %w(true false),
         | 
| 37 | 
            -
                          description: "Return only #{ | 
| 35 | 
            +
                          description: "Return only #{resource_model.to_s.underscore.humanize.downcase.pluralize} which begins in the future"
         | 
| 38 36 | 
             
                        }
         | 
| 39 37 | 
             
                        allowed_keys << :future
         | 
| 40 38 | 
             
                      else
         | 
| 41 39 | 
             
                        additional_filtering_params[name.to_sym] = {
         | 
| 42 40 | 
             
                          proc: -> (value) { value.first == 'true' ? where.not(fi => nil) : where(fi => nil) },
         | 
| 43 41 | 
             
                          all: %w(true false),
         | 
| 44 | 
            -
                          description: "Return only #{name} #{ | 
| 42 | 
            +
                          description: "Return only #{name} #{resource_model.to_s.underscore.humanize.downcase.pluralize}"
         | 
| 45 43 | 
             
                        }
         | 
| 46 44 | 
             
                        allowed_keys << name
         | 
| 47 45 | 
             
                      end
         | 
| @@ -88,7 +86,7 @@ module RiceCooker | |
| 88 86 | 
             
                    custom_filters[name] = {
         | 
| 89 87 | 
             
                      proc: -> (value) { value.first == 'true' ? where.not(field => nil) : where(field => nil) },
         | 
| 90 88 | 
             
                      all: %w(true false),
         | 
| 91 | 
            -
                      description: description || "Return only #{ | 
| 89 | 
            +
                      description: description || "Return only #{resource_model.to_s.underscore.humanize.downcase.pluralize} with a #{field}"
         | 
| 92 90 | 
             
                    }
         | 
| 93 91 | 
             
                    filtering_keys << name
         | 
| 94 92 | 
             
                  end
         | 
    
        data/lib/rice_cooker/helpers.rb
    CHANGED
    
    | @@ -20,50 +20,6 @@ module RiceCooker | |
| 20 20 | 
             
              module Helpers
         | 
| 21 21 | 
             
                extend ActiveSupport::Concern
         | 
| 22 22 |  | 
| 23 | 
            -
                # From https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/class_methods.rb#L315
         | 
| 24 | 
            -
                def controller_resource_class(controller)
         | 
| 25 | 
            -
                  detected_resource_class = nil
         | 
| 26 | 
            -
             | 
| 27 | 
            -
                  if controller.respond_to? :name
         | 
| 28 | 
            -
                    detected_resource_class ||= begin
         | 
| 29 | 
            -
                      namespaced_class = controller.name.sub(/Controller$/, '').singularize
         | 
| 30 | 
            -
                      namespaced_class.constantize
         | 
| 31 | 
            -
                    rescue NameError
         | 
| 32 | 
            -
                      nil
         | 
| 33 | 
            -
                    end
         | 
| 34 | 
            -
             | 
| 35 | 
            -
                    # Second priority is the top namespace model, e.g. EngineName::Article for EngineName::Admin::ArticlesController
         | 
| 36 | 
            -
                    detected_resource_class ||= begin
         | 
| 37 | 
            -
                      namespaced_classes = controller.name.sub(/Controller$/, '').split('::')
         | 
| 38 | 
            -
                      namespaced_class = [namespaced_classes.first, namespaced_classes.last].join('::').singularize
         | 
| 39 | 
            -
                      namespaced_class.constantize
         | 
| 40 | 
            -
                    rescue NameError
         | 
| 41 | 
            -
                      nil
         | 
| 42 | 
            -
                    end
         | 
| 43 | 
            -
             | 
| 44 | 
            -
                    # Third priority the camelcased c, i.e. UserGroup
         | 
| 45 | 
            -
                    detected_resource_class ||= begin
         | 
| 46 | 
            -
                      camelcased_class = controller.name.sub(/Controller$/, '').gsub('::', '').singularize
         | 
| 47 | 
            -
                      camelcased_class.constantize
         | 
| 48 | 
            -
                    rescue NameError
         | 
| 49 | 
            -
                      nil
         | 
| 50 | 
            -
                    end
         | 
| 51 | 
            -
             | 
| 52 | 
            -
                  elsif controller.respond_to? :controller_name
         | 
| 53 | 
            -
                    # Otherwise use the Group class, or fail
         | 
| 54 | 
            -
                    detected_resource_class ||= begin
         | 
| 55 | 
            -
                      class_name = controller.controller_name.classify
         | 
| 56 | 
            -
                      class_name.constantize
         | 
| 57 | 
            -
                    rescue NameError => e
         | 
| 58 | 
            -
                      raise unless e.message.include?(class_name)
         | 
| 59 | 
            -
                      nil
         | 
| 60 | 
            -
                    end
         | 
| 61 | 
            -
                  else
         | 
| 62 | 
            -
                    detected_resource_class = nil
         | 
| 63 | 
            -
                  end
         | 
| 64 | 
            -
                  detected_resource_class
         | 
| 65 | 
            -
                end
         | 
| 66 | 
            -
             | 
| 67 23 | 
             
                # Overridable method for available sortable fields
         | 
| 68 24 | 
             
                def sortable_fields_for(model)
         | 
| 69 25 | 
             
                  if model.respond_to?(:sortable_fields)
         | 
| @@ -102,11 +58,11 @@ module RiceCooker | |
| 102 58 | 
             
                  return {} unless sorting_param.present?
         | 
| 103 59 |  | 
| 104 60 | 
             
                  sorting_params = CSV.parse_line(URI.unescape(sorting_param)).collect do |sort|
         | 
| 105 | 
            -
                    if sort.start_with?('-')
         | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 109 | 
            -
             | 
| 61 | 
            +
                    sorting_param = if sort.start_with?('-')
         | 
| 62 | 
            +
                                      { field: sort[1..-1].to_s.to_sym, direction: :desc }
         | 
| 63 | 
            +
                                    else
         | 
| 64 | 
            +
                                      { field: sort.to_s.to_sym, direction: :asc }
         | 
| 65 | 
            +
                                    end
         | 
| 110 66 |  | 
| 111 67 | 
             
                    check_sorting_param(model, sorting_param)
         | 
| 112 68 | 
             
                    sorting_param
         | 
| @@ -253,7 +209,6 @@ module RiceCooker | |
| 253 209 | 
             
                  raise InvalidRangeException, "Attributes #{🔞.map(&:to_s).to_sentence} doesn't exists or aren't rangeables. Available ranges are: #{allowed.to_sentence}" if 🔞.any?
         | 
| 254 210 | 
             
                end
         | 
| 255 211 |  | 
| 256 | 
            -
             | 
| 257 212 | 
             
                def apply_range_to_collection(collection, ranged_params, additional = {})
         | 
| 258 213 | 
             
                  return collection if collection.nil?
         | 
| 259 214 |  | 
    
        data/lib/rice_cooker/range.rb
    CHANGED
    
    | @@ -13,13 +13,11 @@ module RiceCooker | |
| 13 13 | 
             
                    cattr_accessor :ranged_keys
         | 
| 14 14 | 
             
                    cattr_accessor :custom_ranges
         | 
| 15 15 |  | 
| 16 | 
            -
                    resource_class ||= controller_resource_class(self) unless respond_to?(:resource_class)
         | 
| 17 | 
            -
             | 
| 18 16 | 
             
                    # On normalize tout ca
         | 
| 19 17 | 
             
                    additional_ranged_params = format_additional_param(additional_ranged_params, 'ranged')
         | 
| 20 18 |  | 
| 21 19 | 
             
                    # On recupere tous les filtres autorisés
         | 
| 22 | 
            -
                    allowed_keys = (rangeable_fields_for( | 
| 20 | 
            +
                    allowed_keys = (rangeable_fields_for(resource_model) + additional_ranged_params.keys)
         | 
| 23 21 |  | 
| 24 22 | 
             
                    # On recupere le default
         | 
| 25 23 | 
             
                    self.ranged_keys = allowed_keys
         | 
    
        data/lib/rice_cooker/sort.rb
    CHANGED
    
    | @@ -21,28 +21,27 @@ module RiceCooker | |
| 21 21 | 
             
                    cattr_accessor :default_order
         | 
| 22 22 | 
             
                    cattr_accessor :sorted_keys
         | 
| 23 23 |  | 
| 24 | 
            -
                     | 
| 25 | 
            -
             | 
| 26 | 
            -
                    return unless sorted_keys.nil? || resource_class.nil?
         | 
| 24 | 
            +
                    return unless sorted_keys.nil? || resource_model.nil?
         | 
| 27 25 |  | 
| 28 26 | 
             
                    default_sorting_params = { default_sorting_params => :asc } if default_sorting_params.is_a? Symbol
         | 
| 29 27 |  | 
| 30 28 | 
             
                    # On recupere le default
         | 
| 31 29 | 
             
                    self.default_order = default_sorting_params
         | 
| 32 | 
            -
                    self.sorted_keys = ( | 
| 30 | 
            +
                    self.sorted_keys = (resource_model.respond_to?(:sortable_fields) ? resource_model.sortable_fields : [])
         | 
| 33 31 | 
             
                    default_sort = param_from_defaults(default_sorting_params)
         | 
| 34 32 |  | 
| 35 33 | 
             
                    has_scope :sort, default: default_sort, only: [:index] do |controller, scope, value|
         | 
| 36 | 
            -
                      if controller.params[SORT_PARAM].present?
         | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 34 | 
            +
                      scope = if controller.params[SORT_PARAM].present?
         | 
| 35 | 
            +
                                apply_sort_to_collection(scope, parse_sorting_param(value, resource_model))
         | 
| 36 | 
            +
                              else
         | 
| 37 | 
            +
                                apply_sort_to_collection(scope, default_sorting_params)
         | 
| 38 | 
            +
                              end
         | 
| 41 39 | 
             
                      scope
         | 
| 42 40 | 
             
                    end
         | 
| 43 41 |  | 
| 44 42 | 
             
                  rescue NoMethodError => e
         | 
| 45 43 | 
             
                    "Just wanna die ⚓️ #{e}"
         | 
| 44 | 
            +
                    super
         | 
| 46 45 | 
             
                  end
         | 
| 47 46 | 
             
                end
         | 
| 48 47 | 
             
              end
         | 
    
        data/lib/rice_cooker/version.rb
    CHANGED
    
    
    
        data/lib/rice_cooker.rb
    CHANGED
    
    | @@ -1,17 +1,29 @@ | |
| 1 1 | 
             
            require 'action_controller'
         | 
| 2 2 |  | 
| 3 3 | 
             
            module RiceCooker
         | 
| 4 | 
            -
              autoload :Helpers, | 
| 5 | 
            -
              autoload : | 
| 6 | 
            -
              autoload : | 
| 7 | 
            -
              autoload : | 
| 8 | 
            -
              autoload : | 
| 4 | 
            +
              autoload :Helpers,      'rice_cooker/helpers'
         | 
| 5 | 
            +
              autoload :ClassMethods, 'rice_cooker/class_methods'
         | 
| 6 | 
            +
              autoload :Filter,       'rice_cooker/filter'
         | 
| 7 | 
            +
              autoload :Sort,         'rice_cooker/sort'
         | 
| 8 | 
            +
              autoload :Range,        'rice_cooker/range'
         | 
| 9 | 
            +
              autoload :VERSION,      'rice_cooker/version'
         | 
| 9 10 | 
             
            end
         | 
| 10 11 |  | 
| 11 12 | 
             
            module ActionController
         | 
| 12 13 | 
             
              class Base
         | 
| 13 | 
            -
                 | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 14 | 
            +
                def self.rice_cooked(base)
         | 
| 15 | 
            +
                  base.class_eval do
         | 
| 16 | 
            +
                    include RiceCooker::Sort
         | 
| 17 | 
            +
                    include RiceCooker::Filter
         | 
| 18 | 
            +
                    include RiceCooker::Range
         | 
| 19 | 
            +
                    extend  RiceCooker::ClassMethods
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                    class_attribute :resource_model, instance_writer: false
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                    protected :resource_model
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                rice_cooked(self)
         | 
| 16 28 | 
             
              end
         | 
| 17 29 | 
             
            end
         | 
    
        data/rice_cooker.gemspec
    CHANGED
    
    | @@ -30,13 +30,10 @@ Gem::Specification.new do |s| | |
| 30 30 | 
             
              s.add_development_dependency 'mocha', '1.1.0'
         | 
| 31 31 | 
             
              s.add_development_dependency 'ruby-prof', '0.15.8'
         | 
| 32 32 | 
             
              s.add_development_dependency 'test-unit', '3.1.3'
         | 
| 33 | 
            -
              s.add_development_dependency 'rails-perftest', '0.0.6'
         | 
| 34 33 | 
             
              s.add_development_dependency 'simplecov', '0.11.1'
         | 
| 35 34 | 
             
              s.add_development_dependency 'factory_girl_rails', '~> 4.0'
         | 
| 36 35 | 
             
              s.add_development_dependency 'database_cleaner'
         | 
| 37 36 | 
             
              s.add_development_dependency 'faker', '1.6.1'
         | 
| 38 37 | 
             
              s.add_development_dependency 'pry'
         | 
| 39 | 
            -
              s.add_development_dependency 'derailed'
         | 
| 40 | 
            -
              s.add_development_dependency 'stackprof'
         | 
| 41 38 | 
             
              s.add_development_dependency 'rubocop', '~> 0.40.0'
         | 
| 42 39 | 
             
            end
         | 
    
        data/spec/range/range_spec.rb
    CHANGED
    
    | @@ -90,20 +90,19 @@ RSpec.describe RiceCooker::Range do | |
| 90 90 | 
             
                  # Desc ranged
         | 
| 91 91 | 
             
                  expect do
         | 
| 92 92 | 
             
                    apply_range_to_collection(@collection, login: %w(aaubin qbollach andre),
         | 
| 93 | 
            -
             | 
| 93 | 
            +
                                                           id: ['74'])
         | 
| 94 94 | 
             
                  end.to raise_error(RiceCooker::InvalidRangeValueException)
         | 
| 95 95 | 
             
                end
         | 
| 96 96 | 
             
              end
         | 
| 97 97 |  | 
| 98 98 | 
             
              describe 'Must apply custom ranges to given collection' do
         | 
| 99 | 
            -
             | 
| 100 99 | 
             
                it 'Default null ranged' do
         | 
| 101 100 | 
             
                  ranged_collection = apply_range_to_collection(@collection, {}, @test_range)
         | 
| 102 101 | 
             
                  expect(ranged_collection.to_sql).to match(/^((?!WHERE).)*$/)
         | 
| 103 102 | 
             
                end
         | 
| 104 103 |  | 
| 105 104 | 
             
                it 'Default ranged' do
         | 
| 106 | 
            -
                  ranged_collection = apply_range_to_collection(@collection, {between_the_letter:  | 
| 105 | 
            +
                  ranged_collection = apply_range_to_collection(@collection, { between_the_letter: %w(a b) }, @test_range)
         | 
| 107 106 | 
             
                  expect(ranged_collection.to_sql).to match(/WHERE/)
         | 
| 108 107 | 
             
                  expect(ranged_collection.to_sql).to match(/BETWEEN/)
         | 
| 109 108 | 
             
                end
         | 
| @@ -111,9 +110,9 @@ RSpec.describe RiceCooker::Range do | |
| 111 110 | 
             
                it 'Double ranged' do
         | 
| 112 111 | 
             
                  # Desc ranged
         | 
| 113 112 | 
             
                  ranged_collection = apply_range_to_collection(@collection, {
         | 
| 114 | 
            -
             | 
| 115 | 
            -
             | 
| 116 | 
            -
             | 
| 113 | 
            +
                                                                  between_the_letter: %w(a b),
         | 
| 114 | 
            +
                                                                  not_between_the_letter: %w(x z)
         | 
| 115 | 
            +
                                                                }, @test_range)
         | 
| 117 116 | 
             
                  expect(ranged_collection.to_sql).to match(/WHERE/)
         | 
| 118 117 | 
             
                  expect(ranged_collection.to_sql).to match(/first_name BETWEEN 'a' AND 'b'/)
         | 
| 119 118 | 
             
                  expect(ranged_collection.to_sql).to match(/AND \(first_name NOT BETWEEN 'x' AND 'z'\)/)
         | 
| @@ -121,10 +120,8 @@ RSpec.describe RiceCooker::Range do | |
| 121 120 |  | 
| 122 121 | 
             
                it 'Multiple ranged' do
         | 
| 123 122 | 
             
                  # Desc ranged
         | 
| 124 | 
            -
                  ranged_collection = apply_range_to_collection(@collection,  | 
| 125 | 
            -
             | 
| 126 | 
            -
                    id: ['74', '76']
         | 
| 127 | 
            -
                  })
         | 
| 123 | 
            +
                  ranged_collection = apply_range_to_collection(@collection, login: %w(aaubin qbollach),
         | 
| 124 | 
            +
                                                                             id: %w(74 76))
         | 
| 128 125 | 
             
                  expect(ranged_collection.to_sql).to match(/WHERE/)
         | 
| 129 126 | 
             
                  expect(ranged_collection.to_sql).to match(/BETWEEN 'aaubin' AND 'qbollach'/)
         | 
| 130 127 | 
             
                  expect(ranged_collection.to_sql).to match(/\) AND \(/)
         | 
| @@ -136,75 +133,71 @@ RSpec.describe RiceCooker::Range do | |
| 136 133 | 
             
                  expect do
         | 
| 137 134 | 
             
                    apply_range_to_collection(
         | 
| 138 135 | 
             
                      @collection,
         | 
| 139 | 
            -
                      {sorted:  | 
| 140 | 
            -
                      format_additional_param({sorted: [-> (v,  | 
| 136 | 
            +
                      { sorted: %w(true baguette) },
         | 
| 137 | 
            +
                      format_additional_param({ sorted: [-> (v, _w) { v }, %w(true false maybe)] }, 'ranged')
         | 
| 141 138 | 
             
                    )
         | 
| 142 139 | 
             
                  end.to raise_error(RiceCooker::InvalidRangeValueException)
         | 
| 143 140 | 
             
                end
         | 
| 144 141 | 
             
              end
         | 
| 145 142 |  | 
| 146 143 | 
             
              describe 'Additional params must be correctly formated' do
         | 
| 147 | 
            -
             | 
| 148 144 | 
             
                it 'No additional params' do
         | 
| 149 145 | 
             
                  formated = format_additional_param({}, 'ranged')
         | 
| 150 146 | 
             
                  expect(formated).to be_eql({})
         | 
| 151 147 | 
             
                end
         | 
| 152 148 |  | 
| 153 149 | 
             
                it 'Already correctly formatted additional params' do
         | 
| 154 | 
            -
                  p = {range: {
         | 
| 150 | 
            +
                  p = { range: {
         | 
| 155 151 | 
             
                    proc: @proc,
         | 
| 156 152 | 
             
                    all: [1, 2, 3],
         | 
| 157 153 | 
             
                    description: 'A good filter'
         | 
| 158 | 
            -
                  }}
         | 
| 154 | 
            +
                  } }
         | 
| 159 155 | 
             
                  formated = format_additional_param(p, 'ranged')
         | 
| 160 156 | 
             
                  expect(formated).to be_eql(p)
         | 
| 161 157 | 
             
                end
         | 
| 162 158 |  | 
| 163 159 | 
             
                it 'Missing description additional params' do
         | 
| 164 | 
            -
                  p = {filter: {
         | 
| 160 | 
            +
                  p = { filter: {
         | 
| 165 161 | 
             
                    proc: @proc,
         | 
| 166 162 | 
             
                    all: [1, 2, 3]
         | 
| 167 | 
            -
                  }}
         | 
| 168 | 
            -
                  expected = {filter: {
         | 
| 163 | 
            +
                  } }
         | 
| 164 | 
            +
                  expected = { filter: {
         | 
| 169 165 | 
             
                    proc: @proc,
         | 
| 170 166 | 
             
                    all: [1, 2, 3],
         | 
| 171 167 | 
             
                    description: ''
         | 
| 172 | 
            -
                  }}
         | 
| 168 | 
            +
                  } }
         | 
| 173 169 | 
             
                  formated = format_additional_param(p, 'ranged')
         | 
| 174 170 | 
             
                  expect(formated).to be_eql(expected)
         | 
| 175 171 | 
             
                end
         | 
| 176 172 |  | 
| 177 173 | 
             
                it 'Only proc additional params' do
         | 
| 178 | 
            -
             | 
| 179 | 
            -
                   | 
| 180 | 
            -
                  expected = {filter: {
         | 
| 174 | 
            +
                  p = { filter: @proc }
         | 
| 175 | 
            +
                  expected = { filter: {
         | 
| 181 176 | 
             
                    proc: @proc,
         | 
| 182 177 | 
             
                    all: [],
         | 
| 183 178 | 
             
                    description: ''
         | 
| 184 | 
            -
                  }}
         | 
| 179 | 
            +
                  } }
         | 
| 185 180 | 
             
                  formated = format_additional_param(p, 'ranged')
         | 
| 186 181 | 
             
                  expect(formated).to be_eql(expected)
         | 
| 187 182 | 
             
                end
         | 
| 188 183 |  | 
| 189 184 | 
             
                it 'Array with proc and all additional params' do
         | 
| 190 | 
            -
             | 
| 191 | 
            -
                   | 
| 192 | 
            -
                  expected = {filter: {
         | 
| 185 | 
            +
                  p = { filter: [@proc, @all] }
         | 
| 186 | 
            +
                  expected = { filter: {
         | 
| 193 187 | 
             
                    proc: @proc,
         | 
| 194 188 | 
             
                    all: @all,
         | 
| 195 189 | 
             
                    description: ''
         | 
| 196 | 
            -
                  }}
         | 
| 190 | 
            +
                  } }
         | 
| 197 191 | 
             
                  formated = format_additional_param(p, 'ranged')
         | 
| 198 192 | 
             
                  expect(formated).to be_eql(expected)
         | 
| 199 193 | 
             
                end
         | 
| 200 194 |  | 
| 201 195 | 
             
                it 'Multiple, std + Array with proc and all additional params' do
         | 
| 202 | 
            -
             | 
| 203 196 | 
             
                  p = {
         | 
| 204 197 | 
             
                    tata: @proc,
         | 
| 205 | 
            -
                    toto: {proc: @proc, all: [1, 2]},
         | 
| 198 | 
            +
                    toto: { proc: @proc, all: [1, 2] },
         | 
| 206 199 | 
             
                    filter: [@proc, @all],
         | 
| 207 | 
            -
                    tutu: {proc: @proc, description: 'Buuuuh'}
         | 
| 200 | 
            +
                    tutu: { proc: @proc, description: 'Buuuuh' }
         | 
| 208 201 | 
             
                  }
         | 
| 209 202 | 
             
                  expected = {
         | 
| 210 203 | 
             
                    tata: {
         | 
| @@ -231,7 +224,6 @@ RSpec.describe RiceCooker::Range do | |
| 231 224 | 
             
                  formated = format_additional_param(p, 'ranged')
         | 
| 232 225 | 
             
                  expect(formated).to be_eql(expected)
         | 
| 233 226 | 
             
                end
         | 
| 234 | 
            -
             | 
| 235 227 | 
             
              end
         | 
| 236 228 | 
             
            end
         | 
| 237 229 |  | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: rice_cooker
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.3
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Andre Aubin
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016-07- | 
| 11 | 
            +
            date: 2016-07-06 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rails
         | 
| @@ -188,20 +188,6 @@ dependencies: | |
| 188 188 | 
             
                - - '='
         | 
| 189 189 | 
             
                  - !ruby/object:Gem::Version
         | 
| 190 190 | 
             
                    version: 3.1.3
         | 
| 191 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 192 | 
            -
              name: rails-perftest
         | 
| 193 | 
            -
              requirement: !ruby/object:Gem::Requirement
         | 
| 194 | 
            -
                requirements:
         | 
| 195 | 
            -
                - - '='
         | 
| 196 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 197 | 
            -
                    version: 0.0.6
         | 
| 198 | 
            -
              type: :development
         | 
| 199 | 
            -
              prerelease: false
         | 
| 200 | 
            -
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 201 | 
            -
                requirements:
         | 
| 202 | 
            -
                - - '='
         | 
| 203 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 204 | 
            -
                    version: 0.0.6
         | 
| 205 191 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 206 192 | 
             
              name: simplecov
         | 
| 207 193 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -272,34 +258,6 @@ dependencies: | |
| 272 258 | 
             
                - - ">="
         | 
| 273 259 | 
             
                  - !ruby/object:Gem::Version
         | 
| 274 260 | 
             
                    version: '0'
         | 
| 275 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 276 | 
            -
              name: derailed
         | 
| 277 | 
            -
              requirement: !ruby/object:Gem::Requirement
         | 
| 278 | 
            -
                requirements:
         | 
| 279 | 
            -
                - - ">="
         | 
| 280 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 281 | 
            -
                    version: '0'
         | 
| 282 | 
            -
              type: :development
         | 
| 283 | 
            -
              prerelease: false
         | 
| 284 | 
            -
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 285 | 
            -
                requirements:
         | 
| 286 | 
            -
                - - ">="
         | 
| 287 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 288 | 
            -
                    version: '0'
         | 
| 289 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 290 | 
            -
              name: stackprof
         | 
| 291 | 
            -
              requirement: !ruby/object:Gem::Requirement
         | 
| 292 | 
            -
                requirements:
         | 
| 293 | 
            -
                - - ">="
         | 
| 294 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 295 | 
            -
                    version: '0'
         | 
| 296 | 
            -
              type: :development
         | 
| 297 | 
            -
              prerelease: false
         | 
| 298 | 
            -
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 299 | 
            -
                requirements:
         | 
| 300 | 
            -
                - - ">="
         | 
| 301 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 302 | 
            -
                    version: '0'
         | 
| 303 261 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 304 262 | 
             
              name: rubocop
         | 
| 305 263 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -330,6 +288,7 @@ files: | |
| 330 288 | 
             
            - README.md
         | 
| 331 289 | 
             
            - Rakefile
         | 
| 332 290 | 
             
            - lib/rice_cooker.rb
         | 
| 291 | 
            +
            - lib/rice_cooker/class_methods.rb
         | 
| 333 292 | 
             
            - lib/rice_cooker/filter.rb
         | 
| 334 293 | 
             
            - lib/rice_cooker/helpers.rb
         | 
| 335 294 | 
             
            - lib/rice_cooker/range.rb
         |