active_enum 1.2.0 → 1.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96e647ed19a3af115aa962e61aaecb6b254d51064a60c86a14a4735b32d264d5
4
- data.tar.gz: c4d6f20b518d1b8171780048624f36f657f276cd996b78f6fec7ef4d99f7dcdc
3
+ metadata.gz: cef2baaca824d0aabf253fc18b8316ee90c0cb55f4ab9c1d64d636ad32da8fdd
4
+ data.tar.gz: a5e4fbed89a43e84520cf82561b92be85ef4165a444c3f4406f5f234f06fc336
5
5
  SHA512:
6
- metadata.gz: af70d47df41c0ad90a99ca11691609e8812034acbaac5b389ec07542c0e8eed326657351de923eeeb60e9a15a60adadb726186f114a511952e17afd71bc0433b
7
- data.tar.gz: 76d5265b1205a28435c857a2b108bfb4f36d1cfd2bd3bf1dbe5a99594e0b10b9828f2a7a7bdfb1d4d2e834372fc122912e01939ca22fedd60500692cffc2c929
6
+ metadata.gz: 65517f34358ae160cadfe3ef4154bc9e4af1354dbe3b0f1d214322f53fbc11e4b53a63f77cb11bf1c7f418448bd9dce168bc4fb004b68b636764b6adf4ce691b
7
+ data.tar.gz: 6324aa433ff7737a0d1a2092d7d288bbd64a17ddbdeb3f9a646de24230e2eb869f2d38a6b86aa1bc5f5f32a4a01b4a8cbba72a5033a5e36d7d67a2b28789e6e6
data/Appraisals ADDED
@@ -0,0 +1,19 @@
1
+ appraise "rails_7_0" do
2
+ gem "rails", "~> 7.0.0"
3
+ gem 'sqlite3', '~> 1.7.0'
4
+ end
5
+
6
+ appraise "rails_7_1" do
7
+ gem "rails", "~> 7.1.0"
8
+ gem 'sqlite3', '~> 2.6.0'
9
+ end
10
+
11
+ appraise "rails_7_2" do
12
+ gem "rails", "~> 7.2.0"
13
+ gem 'sqlite3', '~> 2.6.0'
14
+ end
15
+
16
+ appraise "rails_8_0" do
17
+ gem "rails", "~> 8.0.0"
18
+ gem 'sqlite3', '~> 2.6.0'
19
+ end
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rails", "~> 6.1.0"
7
+ gem "sqlite3", "~> 1.4.0"
8
+ gem "combustion"
9
+ gem "rake"
10
+ gem "simple_form", "~> 5.0.0"
11
+ gem "debug"
12
+ gem "rspec", "~> 3.4"
13
+ gem "rspec-rails", "~> 6.0"
14
+ gem "capybara"
15
+
16
+ gemspec path: "../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rails", "~> 7.0.0"
7
+ gem "sqlite3", "~> 1.7.0"
8
+ gem "combustion"
9
+ gem "rake"
10
+ gem "simple_form", "~> 5.0.0"
11
+ gem "debug"
12
+ gem "rspec", "~> 3.4"
13
+ gem "rspec-rails", "~> 6.0"
14
+ gem "capybara"
15
+
16
+ gemspec path: "../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rails", "~> 7.1.0"
7
+ gem "sqlite3", "~> 2.6.0"
8
+ gem "combustion"
9
+ gem "rake"
10
+ gem "simple_form", "~> 5.0.0"
11
+ gem "debug"
12
+ gem "rspec", "~> 3.4"
13
+ gem "rspec-rails", "~> 6.0"
14
+ gem "capybara"
15
+
16
+ gemspec path: "../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rails", "~> 7.2.0"
7
+ gem "sqlite3", "~> 2.6.0"
8
+ gem "combustion"
9
+ gem "rake"
10
+ gem "simple_form", "~> 5.0.0"
11
+ gem "debug"
12
+ gem "rspec", "~> 3.4"
13
+ gem "rspec-rails", "~> 6.0"
14
+ gem "capybara"
15
+
16
+ gemspec path: "../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rails", "~> 8.0.0"
7
+ gem "sqlite3", "~> 2.6.0"
8
+ gem "combustion"
9
+ gem "rake"
10
+ gem "simple_form", "~> 5.0.0"
11
+ gem "debug"
12
+ gem "rspec", "~> 3.4"
13
+ gem "rspec-rails", "~> 6.0"
14
+ gem "capybara"
15
+
16
+ gemspec path: "../"
@@ -6,15 +6,17 @@ module ActiveEnum
6
6
  def acts_as_enum(options={})
7
7
  extend ClassMethods
8
8
  class_attribute :active_enum_options
9
- self.active_enum_options = options.reverse_merge(:name_column => 'name')
10
- scope :enum_values, proc { select(Arel.sql("#{primary_key}, #{active_enum_options[:name_column]}")).
11
- where(active_enum_options[:conditions]).
12
- order(Arel.sql("#{primary_key} #{active_enum_options[:order]}")) }
9
+ self.active_enum_options = options.reverse_merge(name_column: 'name')
13
10
  end
14
11
 
15
12
  end
16
13
 
17
14
  module ClassMethods
15
+ def enum_values
16
+ select(Arel.sql("#{primary_key}, #{active_enum_options[:name_column]}"))
17
+ .where(active_enum_options[:conditions])
18
+ .order(Arel.sql("#{primary_key} #{active_enum_options[:order]}"))
19
+ end
18
20
 
19
21
  def values
20
22
  enum_values.map { |v| [ v.id, v.send(active_enum_options[:name_column]) ] }
@@ -33,14 +35,19 @@ module ActiveEnum
33
35
  end
34
36
 
35
37
  def [](index)
36
- row = get_value(index)
38
+ get(index)
39
+ end
40
+
41
+ def get(index, raise_on_not_found: ActiveEnum.raise_on_not_found)
42
+ row = get_value(index, raise_on_not_found)
37
43
  return if row.nil?
38
44
  index.is_a?(Integer) ? row.send(active_enum_options[:name_column]) : row.id
39
45
  end
40
46
 
41
47
  # Access any meta data defined for a given id or name. Returns a hash.
42
- def meta(index)
48
+ def meta(index, raise_on_not_found: ActiveEnum.raise_on_not_found)
43
49
  row = lookup_relation(index).unscope(:select).first
50
+ raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if raise_on_not_found
44
51
  row&.attributes.except(primary_key.to_s, active_enum_options[:name_column].to_s)
45
52
  end
46
53
 
@@ -53,17 +60,17 @@ module ActiveEnum
53
60
 
54
61
  private
55
62
 
56
- def get_value(index)
63
+ def get_value(index, raise_on_not_found = ActiveEnum.raise_on_not_found)
57
64
  lookup_relation(index).first || begin
58
- raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if ActiveEnum.raise_on_not_found
65
+ raise(ActiveEnum::NotFound, "#{self} value for '#{index}' was not found") if raise_on_not_found
59
66
  end
60
67
  end
61
-
68
+
62
69
  def lookup_relation(index)
63
70
  if index.is_a?(Integer)
64
71
  enum_values.where(id: index)
65
72
  else
66
- enum_values.where("lower(#{active_enum_options[:name_column]}) = lower(?)", Arel.sql(index.to_s))
73
+ enum_values.where("lower(#{active_enum_options[:name_column]}) = lower(:name)", name: index.to_s)
67
74
  end
68
75
  end
69
76
  end
@@ -5,7 +5,7 @@ module ActiveEnum
5
5
 
6
6
  class Base
7
7
  class << self
8
- attr_accessor :store
8
+ attr_reader :store
9
9
 
10
10
  def inherited(subclass)
11
11
  ActiveEnum.enum_classes << subclass
@@ -26,6 +26,8 @@ module ActiveEnum
26
26
  # Allowed values are :asc, :desc or :natural
27
27
  #
28
28
  def order(order)
29
+ raise "Invalid order '#{order}' in #{self}" unless order.in?([:asc, :desc, :as_defined, :natural])
30
+
29
31
  if order == :as_defined
30
32
  ActiveSupport::Deprecation.warn("You are using the order :as_defined which has been deprecated. Use :natural.")
31
33
  order = :natural
@@ -87,9 +89,7 @@ module ActiveEnum
87
89
  end
88
90
 
89
91
  def [](index)
90
- row = get_value(index)
91
- return if row.nil?
92
- index.is_a?(Integer) ? row[1] : row[0]
92
+ get(index)
93
93
  end
94
94
 
95
95
  def include?(value)
@@ -43,7 +43,7 @@ module ActiveEnum
43
43
 
44
44
  define_active_enum_methods_for_attribute(attribute, options) unless options[:skip_accessors]
45
45
  rescue NameError => e
46
- raise e unless e.message =~ /uninitialized constant/
46
+ raise e unless e.message.match?(/uninitialized constant/)
47
47
  raise ActiveEnum::EnumNotFound, "Enum class could not be found for attribute '#{attribute}' in class #{self}. Specify the enum class using the :with option."
48
48
  end
49
49
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -31,20 +31,43 @@ describe ActiveEnum::ActsAsEnum do
31
31
  end
32
32
 
33
33
  context "#[]" do
34
- it "should return name column value when passing id to [] method" do
34
+ it "should return name column value when passed id" do
35
35
  expect(Person[1]).to eq('Dave')
36
36
  end
37
37
 
38
- it "should return id column value when passing string name to [] method" do
38
+ it "should return id column value when passed string name" do
39
39
  expect(Person['Dave']).to eq(1)
40
40
  expect(Person['dave']).to eq(1)
41
41
  end
42
42
 
43
- it "should return id column value when passing symbol name to [] method" do
43
+ it "should return id column value when passed symbol name" do
44
44
  expect(Person[:dave]).to eq(1)
45
45
  end
46
46
  end
47
47
 
48
+ context '#get' do
49
+ it "should return name column value when passed id" do
50
+ expect(Person.get(1)).to eq('Dave')
51
+ end
52
+
53
+ it "should return id column value when passed string name" do
54
+ expect(Person.get('Dave')).to eq(1)
55
+ expect(Person.get('dave')).to eq(1)
56
+ end
57
+
58
+ it "should return id column value when passed symbol name" do
59
+ expect(Person.get(:dave)).to eq(1)
60
+ end
61
+
62
+ it "should not raise for missing id when raise_on_not_found is false" do
63
+ expect { Person.get(0, raise_on_not_found: false) }.to_not raise_error
64
+ end
65
+
66
+ it "should raise for missing id when raise_on_not_found is true" do
67
+ expect { Person.get(0, raise_on_not_found: true) }.to raise_error(ActiveEnum::NotFound)
68
+ end
69
+ end
70
+
48
71
  context '#values' do
49
72
  it "should return array of arrays containing id and name column values" do
50
73
  expect(Person.values).to eq([[1, 'Dave'], [2, 'John']])
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  ENV["RAILS_ENV"] ||= 'test'
2
- require 'spec_helper'
3
2
 
3
+ require 'logger'
4
4
  require 'rails'
5
5
  require 'active_record'
6
6
  require 'action_controller/railtie'
@@ -9,21 +9,16 @@ require 'action_mailer'
9
9
 
10
10
  require 'active_enum'
11
11
  require 'active_enum/acts_as_enum'
12
- require 'securerandom'
13
- require 'byebug'
14
12
 
15
- module ActiveEnum
13
+ module ActiveEnum
16
14
  class Application < Rails::Application
17
15
  config.generators do |g|
18
16
  g.orm :active_record
19
17
  g.test_framework :rspec, :fixture => false
20
18
  end
21
19
  config.active_support.deprecation = :notify
22
- config.eager_load = false if Rails.version >= "4.0"
23
- config.secret_key_base = SecureRandom.hex(10) if Rails.version >= "4.0"
24
20
  end
25
21
  end
26
- ActiveEnum::Application.initialize!
27
22
 
28
23
  I18n.enforce_available_locales = false
29
24
  I18n.available_locales = ['en', 'ja']
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-02-13 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activesupport
@@ -29,20 +28,22 @@ email: adam.meehan@gmail.com
29
28
  executables: []
30
29
  extensions: []
31
30
  extra_rdoc_files:
32
- - README.rdoc
33
31
  - CHANGELOG
34
32
  - MIT-LICENSE
33
+ - README.rdoc
35
34
  files:
36
35
  - ".travis.yml"
36
+ - Appraisals
37
37
  - CHANGELOG
38
38
  - MIT-LICENSE
39
39
  - README.rdoc
40
40
  - Rakefile
41
41
  - active_enum.gemspec
42
- - gemfiles/rails_4_2.gemfile
43
- - gemfiles/rails_5_0.gemfile
44
- - gemfiles/rails_5_1.gemfile
45
- - gemfiles/rails_5_2.gemfile
42
+ - gemfiles/rails_6_1.gemfile
43
+ - gemfiles/rails_7_0.gemfile
44
+ - gemfiles/rails_7_1.gemfile
45
+ - gemfiles/rails_7_2.gemfile
46
+ - gemfiles/rails_8_0.gemfile
46
47
  - init.rb
47
48
  - lib/active_enum.rb
48
49
  - lib/active_enum/acts_as_enum.rb
@@ -72,7 +73,6 @@ files:
72
73
  homepage: http://github.com/adzap/active_enum
73
74
  licenses: []
74
75
  metadata: {}
75
- post_install_message:
76
76
  rdoc_options: []
77
77
  require_paths:
78
78
  - lib
@@ -87,8 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
- rubygems_version: 3.5.6
91
- signing_key:
90
+ rubygems_version: 3.6.8
92
91
  specification_version: 4
93
92
  summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
94
93
  test_files:
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 4.2.11"
6
-
7
- gem 'sqlite3', '~> 1.3.0'
8
-
9
- gem 'rake'
10
- gem 'simple_form', '~> 3.0'
11
- gem 'byebug'
12
- gem 'rspec', '~> 3.4'
13
- gem 'rspec-rails', '~> 3.4'
14
- gem 'capybara'
15
-
16
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 5.1.0"
6
-
7
- gem 'sqlite3', '~> 1.3.0'
8
-
9
- gem 'rake'
10
- gem 'simple_form', '~> 5.0'
11
- gem 'byebug'
12
- gem 'rspec', '~> 3.4'
13
- gem 'rspec-rails', '~> 3.4'
14
- gem 'capybara'
15
-
16
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 5.1.0"
6
-
7
- gem 'sqlite3', '~> 1.3.0'
8
-
9
- gem 'rake'
10
- gem 'simple_form', '~> 5.0'
11
- gem 'byebug'
12
- gem 'rspec', '~> 3.4'
13
- gem 'rspec-rails', '~> 3.4'
14
- gem 'capybara'
15
-
16
- gemspec path: "../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 5.2.0"
6
-
7
- gem 'sqlite3', '~> 1.3.0'
8
-
9
- gem 'rake'
10
- gem 'simple_form', '~> 5.0'
11
- gem 'byebug'
12
- gem 'rspec', '~> 3.4'
13
- gem 'rspec-rails', '~> 3.4'
14
- gem 'capybara'
15
-
16
- gemspec path: "../"