enumish 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd34d003bdef7eaf92786ab4e6e1bd25a1ca7cab
4
- data.tar.gz: '092c70c046c113815500f169ffa669b84a225a72'
3
+ metadata.gz: c3996fbc95c17dbba17a1cdd9c19316a1b744a02
4
+ data.tar.gz: 0c39957e59b88433056262331370dfd761fa1a7a
5
5
  SHA512:
6
- metadata.gz: 4fe669e7dc94447fb27d92907bfc40cff10fa88baf82e97c9cb72a92efc3549dfdc217cbdf0f3d7063325578c7d35acd3dcb651d357dedb1b0a867fc7cfb1bd2
7
- data.tar.gz: c5d7069c7aa3c1f67c26262559af0f9932afee34ec3a39e618f3066342ea2eda9c115071248b2fa132725e3f7bdcdc2f09db914b640a11236e58e24667d2277a
6
+ metadata.gz: 0c64c8aa0568c57d7e167ed6e451615ccbd53657bd12ee5a91e2587d3c243d861072cdcd04968e4b6253bfa5a73c1706343a91283644475cac6c5aa7cf2b9e95
7
+ data.tar.gz: de7732dcc20673dca3559b0854929c6584181e89873445ac2a61aa000497bf1ca42abe7e611680bfe092e9841beb8b6a61bc764b91494b8fc77a094161a54013
data/.gitignore CHANGED
@@ -10,3 +10,5 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ .byebug_history
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Enumish
2
2
 
3
- Enumish is "Enum for ActiveRecord".
3
+ Enumish is "Database-backed Enum for ActiveRecord".
4
4
 
5
- It gives you the ability to create Enum-like values directly in the database.
5
+ It gives you the ability to create Enum-like properties directly in the database.
6
+
7
+ [![Build Status](https://travis-ci.org/cmer/enumish.svg?branch=master)](https://travis-ci.org/cmer/enumish)
6
8
 
7
9
  ## Simple Example
8
10
 
@@ -44,10 +46,14 @@ class Color < ActiveRecord
44
46
  { s.human_description => s.id }
45
47
  end
46
48
  end
49
+
50
+ def all_primary
51
+ Color.rank(:position).where(enabled: true).where(primary: true)
52
+ end
47
53
  end
48
54
 
49
- def description
50
- "#{self.to_s} is a beautiful color!"
55
+ def human_description
56
+ "The color #{self.to_s}"
51
57
  end
52
58
  end
53
59
  ````
@@ -108,6 +114,34 @@ class Color < ActiveRecord
108
114
  end
109
115
  ````
110
116
 
117
+ ## Why not just use ActiveRecord::Enum?
118
+
119
+ Glad you asked! [`ActiveRecord::Enum`](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html)
120
+ (AR::E going forward) is an option you should definitely consider if it fits your needs. I wrote
121
+ Enumish because I needed the following capabilities:
122
+
123
+ - Ability to have more than one enum field per model.
124
+
125
+ For example, a `Car` model could have the following Enumish properties:
126
+ `exterior_color`, `interior_color`, `model`, `transmission`. This would not be possible
127
+ with vanilla AR::E.
128
+
129
+ - Database-backed properties. There are many benefits to this. Notably, Enumish allows you to:
130
+
131
+ - Maintain an ever-evolving list of properties directly in the database instead of changing
132
+ and potentially breaking code. Your marketing team will love this!
133
+ - Keep a human-readable version of your Enumish properties in the database for your users
134
+ to see, or for your eyes only. Just add a `description` column.
135
+ - Enable or disable some properties. Just add an `enabled` column.
136
+ - Sort how properties are displayed to end-users. Pro-tip, just use
137
+ [`RankedModel`](https://github.com/mixonic/ranked-model).
138
+ - Limit possible Enumish properties under different circumstances. For example, a car from 2015
139
+ might have different color options than a car from 2016. Or in the example above, I have
140
+ an `.all_primary` method that only returns primary colors.
141
+ - Perform database `JOIN`s between your model and Enumish models.
142
+ - Maintain data integrity between your models and Enumish models. It's all just SQL after all.
143
+
144
+
111
145
  ## Installation
112
146
 
113
147
  Add this line to your application's Gemfile:
@@ -8,7 +8,7 @@ module Enumish
8
8
 
9
9
  module ClassMethods
10
10
  def method_missing(method_id, *args, &block)
11
- if !method_id.to_s.match(/\?$/)
11
+ if !method_id.to_s.match(/\?$/) && enum_ids.include?(method_id.to_s)
12
12
  obj = self.where(enum_id => method_id.to_s).first
13
13
  return obj if obj.present?
14
14
  end
@@ -19,12 +19,27 @@ module Enumish
19
19
  def enum_id
20
20
  :short
21
21
  end
22
+
23
+ def refresh_enum_ids!
24
+ Mutex.new.synchronize do
25
+ @enum_ids = self.pluck(enum_id)
26
+ end
27
+ end
28
+
29
+ def enum_ids
30
+ refresh_enum_ids! if @enum_ids.blank?
31
+ @enum_ids
32
+ end
22
33
  end
23
34
 
24
35
  # Allow calls such as object.friendly? or model.attitude.friendly?
25
36
  def method_missing(method_id, *args, &block)
26
- if method_id.to_s.match(/\?$/) && args.empty? && block.nil?
27
- self.send(self.class.enum_id.to_s) == method_id.to_s.sub(/\?$/, "")
37
+ bare_method = if method_id.to_s.match(/\?$/) && args.empty? && block.nil?
38
+ method_id.to_s.sub(/\?$/, "")
39
+ end
40
+
41
+ if bare_method && self.class.enum_ids.include?(bare_method)
42
+ self.send(self.class.enum_id.to_s) == bare_method
28
43
  else
29
44
  super
30
45
  end
@@ -1,3 +1,3 @@
1
1
  module Enumish
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumish
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Mercier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-11 00:00:00.000000000 Z
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler