~mrls/activerecord-except

7a5541a4540035581a0a30f50d62d22987de03af — Mauro Morales 11 months ago master
Initial commit
A  => .gitignore +10 -0
@@ 1,10 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
tags
Gemfile.lock

A  => .travis.yml +6 -0
@@ 1,6 @@
---
language: ruby
cache: bundler
rvm:
  - 2.6.6
before_install: gem install bundler -v 2.1.4

A  => Gemfile +8 -0
@@ 1,8 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

gem 'minitest', '~> 5.0'
gem 'rake', '~> 12.0'

A  => LICENSE.txt +21 -0
@@ 1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Mauro Morales

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

A  => README.md +41 -0
@@ 1,41 @@
# Activerecord::Except

Provides `ActiveRecord::Relation#except`, a convenience method that let's you
select all the fields from a table with the exception of the given ones.

## Installation

Add this line to your Rails' application Gemfile:

```ruby
gem 'activerecord-except'
```

And then execute:

    $ bundle install

## Usage

```ruby
User.all.except(:unwanted_field_name)
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run
`rake test` to run the tests. You can also run `bin/console` for an interactive
prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To
release a new version, update the version number in `version.rb`, and then run
`bundle exec rake release`, which will create a git tag for the version, push
git commits and tags, and push the `.gem` file to
[rubygems.org](https://rubygems.org).



## License

The gem is available as open source under the terms of the [MIT
License](https://opensource.org/licenses/MIT).

A  => Rakefile +12 -0
@@ 1,12 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
  t.libs << 'test'
  t.libs << 'lib'
  t.test_files = FileList['test/**/*_test.rb']
end

task default: :test

A  => activerecord-except.gemspec +38 -0
@@ 1,38 @@
# frozen_string_literal: true

require_relative 'lib/activerecord/except/version'

Gem::Specification.new do |spec|
  spec.name          = 'activerecord-except'
  spec.version       = Activerecord::Except::VERSION
  spec.authors       = ['Mauro Morales']
  spec.email         = ['contact@mauromorales.com']

  spec.summary       = 'Select all fields from a table with the exception of the given ones'
  spec.description   = <<~TEXT
    Provides `ActiveRecord::Relation#except`, a convenience method that let's
    you select all the fields from a table with the exception of the given
    ones.
  TEXT
  spec.homepage      = 'https://git.sr.ht/~mrls/activerecord-except'
  spec.license       = 'MIT'
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')

  spec.metadata['homepage_uri'] = spec.homepage
  spec.metadata['source_code_uri'] = 'https://git.sr.ht/~mrls/activerecord-except'

  # Specify which files should be added to the gem when it is released.
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  end
  spec.bindir        = 'exe'
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ['lib']

  spec.add_dependency 'activerecord', '>= 4'

  spec.add_development_dependency 'minitest'
  spec.add_development_dependency 'rake'
  spec.add_development_dependency 'sqlite3'
end

A  => bin/console +15 -0
@@ 1,15 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'activerecord/except'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start(__FILE__)

A  => bin/setup +8 -0
@@ 1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here

A  => lib/activerecord/except.rb +10 -0
@@ 1,10 @@
# frozen_string_literal: true

require 'activerecord/except/relation'
require 'activerecord/except/version'

module Activerecord
  module Except
    class Error < StandardError; end
  end
end

A  => lib/activerecord/except/relation.rb +10 -0
@@ 1,10 @@
# frozen_string_literal: true

module ActiveRecord
  class Relation
    def except(*fields)
      inverted_fields = klass._default_attributes.keys.map(&:to_sym).reject { |attr| fields.include?(attr) }
      _select!(inverted_fields)
    end
  end
end

A  => lib/activerecord/except/version.rb +7 -0
@@ 1,7 @@
# frozen_string_literal: true

module Activerecord
  module Except
    VERSION = '0.1.0'
  end
end

A  => test/activerecord/except_test.rb +37 -0
@@ 1,37 @@
# frozen_string_literal: true

require 'active_record'
require 'test_helper'

class User < ActiveRecord::Base
end

class ExceptTest < Minitest::Test
  def setup
    ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

    ActiveRecord::Schema.define do
      create_table :users do |t|
        t.string :first_name
        t.string :last_name
        t.string :address
        t.string :file
      end
    end

    User.create!(first_name: 'Max',
                 last_name: 'Musterman',
                 address: 'Berlin',
                 file: 'avatar.jpg')
  end

  def test_except
    user = User.where(address: 'Berlin').except(:file).first

    # implied assertion that no exceptions are raised
    assert_equal user.first_name, 'Max'
    assert_equal user.last_name, 'Musterman'
    assert_equal user.address, 'Berlin'
    assert_raises(NoMethodError) { user.file }
  end
end

A  => test/test_helper.rb +6 -0
@@ 1,6 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'activerecord/except'

require 'minitest/autorun'