inter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +102 -0
- data/LICENSE +23 -0
- data/README.md +176 -0
- data/Rakefile +9 -0
- data/inter.gemspec +25 -0
- data/lib/generators/inter/install_generator.rb +15 -0
- data/lib/generators/templates/create_interactions.rb +13 -0
- data/lib/inter.rb +2 -0
- data/lib/inter/actable.rb +172 -0
- data/lib/inter/action.rb +26 -0
- data/lib/inter/version.rb +3 -0
- data/spec/class_spec.rb +38 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +16 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/article.rb +3 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +24 -0
- data/spec/dummy/config/boot.rb +4 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20131111155047_create_interactions.rb +13 -0
- data/spec/dummy/db/migrate/20131111233640_create_articles.rb +7 -0
- data/spec/dummy/db/schema.rb +30 -0
- data/spec/dummy/db/seeds.rb +7 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/lib/tasks/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/log/development.log +20 -0
- data/spec/dummy/log/test.log +41749 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/robots.txt +5 -0
- data/spec/dummy/test/controllers/.keep +0 -0
- data/spec/dummy/test/fixtures/.keep +0 -0
- data/spec/dummy/test/helpers/.keep +0 -0
- data/spec/dummy/test/integration/.keep +0 -0
- data/spec/dummy/test/mailers/.keep +0 -0
- data/spec/dummy/test/models/.keep +0 -0
- data/spec/dummy/test/test_helper.rb +15 -0
- data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
- data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
- data/spec/instance_spec.rb +337 -0
- data/spec/related_spec.rb +43 -0
- data/spec/spec_helper.rb +15 -0
- metadata +240 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b72b65086332d83f591e0ace10b28c72d0b798eb
|
4
|
+
data.tar.gz: 9fd22162c416d7a85876a6b191e0fdeb8ba31965
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5119c156bdec468782df041b245d1e8c5d1115f12f955d706f26232be528ecf99cb782c6b245e441e1e8d01481247671d4e0364621579a5201416bbb45d506c
|
7
|
+
data.tar.gz: ff433a0f9b9ad5d38c5876053e9417c86ebd8517ca681dd4c61813402b69b464972f65d6e0677bbe9ef3ccf32c52fb35eeb7e426b526483c40c7fb3f5ecaa6c5
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/*
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
interactable
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
inter (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionmailer (4.0.1)
|
10
|
+
actionpack (= 4.0.1)
|
11
|
+
mail (~> 2.5.4)
|
12
|
+
actionpack (4.0.1)
|
13
|
+
activesupport (= 4.0.1)
|
14
|
+
builder (~> 3.1.0)
|
15
|
+
erubis (~> 2.7.0)
|
16
|
+
rack (~> 1.5.2)
|
17
|
+
rack-test (~> 0.6.2)
|
18
|
+
activemodel (4.0.1)
|
19
|
+
activesupport (= 4.0.1)
|
20
|
+
builder (~> 3.1.0)
|
21
|
+
activerecord (4.0.1)
|
22
|
+
activemodel (= 4.0.1)
|
23
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
24
|
+
activesupport (= 4.0.1)
|
25
|
+
arel (~> 4.0.0)
|
26
|
+
activerecord-deprecated_finders (1.0.3)
|
27
|
+
activesupport (4.0.1)
|
28
|
+
i18n (~> 0.6, >= 0.6.4)
|
29
|
+
minitest (~> 4.2)
|
30
|
+
multi_json (~> 1.3)
|
31
|
+
thread_safe (~> 0.1)
|
32
|
+
tzinfo (~> 0.3.37)
|
33
|
+
arel (4.0.1)
|
34
|
+
atomic (1.1.14)
|
35
|
+
builder (3.1.4)
|
36
|
+
diff-lcs (1.2.5)
|
37
|
+
erubis (2.7.0)
|
38
|
+
hike (1.2.3)
|
39
|
+
i18n (0.6.5)
|
40
|
+
mail (2.5.4)
|
41
|
+
mime-types (~> 1.16)
|
42
|
+
treetop (~> 1.4.8)
|
43
|
+
mime-types (1.25)
|
44
|
+
minitest (4.7.5)
|
45
|
+
multi_json (1.8.2)
|
46
|
+
polyglot (0.3.3)
|
47
|
+
rack (1.5.2)
|
48
|
+
rack-test (0.6.2)
|
49
|
+
rack (>= 1.0)
|
50
|
+
rails (4.0.1)
|
51
|
+
actionmailer (= 4.0.1)
|
52
|
+
actionpack (= 4.0.1)
|
53
|
+
activerecord (= 4.0.1)
|
54
|
+
activesupport (= 4.0.1)
|
55
|
+
bundler (>= 1.3.0, < 2.0)
|
56
|
+
railties (= 4.0.1)
|
57
|
+
sprockets-rails (~> 2.0.0)
|
58
|
+
railties (4.0.1)
|
59
|
+
actionpack (= 4.0.1)
|
60
|
+
activesupport (= 4.0.1)
|
61
|
+
rake (>= 0.8.7)
|
62
|
+
thor (>= 0.18.1, < 2.0)
|
63
|
+
rake (10.1.0)
|
64
|
+
rspec-core (2.14.7)
|
65
|
+
rspec-expectations (2.14.4)
|
66
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
67
|
+
rspec-mocks (2.14.4)
|
68
|
+
rspec-rails (2.14.0)
|
69
|
+
actionpack (>= 3.0)
|
70
|
+
activesupport (>= 3.0)
|
71
|
+
railties (>= 3.0)
|
72
|
+
rspec-core (~> 2.14.0)
|
73
|
+
rspec-expectations (~> 2.14.0)
|
74
|
+
rspec-mocks (~> 2.14.0)
|
75
|
+
sprockets (2.10.0)
|
76
|
+
hike (~> 1.2)
|
77
|
+
multi_json (~> 1.0)
|
78
|
+
rack (~> 1.0)
|
79
|
+
tilt (~> 1.1, != 1.3.0)
|
80
|
+
sprockets-rails (2.0.1)
|
81
|
+
actionpack (>= 3.0)
|
82
|
+
activesupport (>= 3.0)
|
83
|
+
sprockets (~> 2.8)
|
84
|
+
sqlite3 (1.3.8)
|
85
|
+
thor (0.18.1)
|
86
|
+
thread_safe (0.1.3)
|
87
|
+
atomic
|
88
|
+
tilt (1.4.1)
|
89
|
+
treetop (1.4.15)
|
90
|
+
polyglot
|
91
|
+
polyglot (>= 0.3.1)
|
92
|
+
tzinfo (0.3.38)
|
93
|
+
|
94
|
+
PLATFORMS
|
95
|
+
ruby
|
96
|
+
|
97
|
+
DEPENDENCIES
|
98
|
+
inter!
|
99
|
+
rails
|
100
|
+
rake
|
101
|
+
rspec-rails
|
102
|
+
sqlite3
|
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2013 Cristiano Betta <cbetta@gmail.com>
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
data/README.md
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
# Inter
|
2
|
+
|
3
|
+
Easily add arbitrary attributes to an ActiveRecord model on the fly, without the need of any extra new database columns or migrations.
|
4
|
+
|
5
|
+
## Why?
|
6
|
+
|
7
|
+
Ever added a 5th boolean to a model just so you could keep track if _something_ had happened to this user, article, etc? With __Inter__ you can add any arbitrary attributes to any ActiveRecord model on the fly without the need to add yet another database migration. Instead all __interactions__ are stored in a seperate table and hooked up using polymorphism. Using a simple DSL you can then quickly manipulate and query interactions.
|
8
|
+
|
9
|
+
Benefits:
|
10
|
+
|
11
|
+
* Cleaner models
|
12
|
+
* Simple and powerful DSL
|
13
|
+
* More lightweight than a full state machine
|
14
|
+
* Can store any value
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
## Dependencies
|
19
|
+
|
20
|
+
* >= Ruby 2.0
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add the gem to your Gemfile.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem "inter"
|
28
|
+
```
|
29
|
+
|
30
|
+
Then generate and run the database migrations.
|
31
|
+
|
32
|
+
```shell
|
33
|
+
rails generate inter:install
|
34
|
+
rake db:migrate
|
35
|
+
```
|
36
|
+
|
37
|
+
And finally define an ActiveRecord model as an interactable.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class Article < ActiveRecord::Base
|
41
|
+
include Inter::Actable
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
## Basic usage
|
46
|
+
|
47
|
+
### Instance methods
|
48
|
+
|
49
|
+
Setting a value on an Article is now easy. Just pick a key name and set it to a desired value.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# simple boolean setting and getting2
|
53
|
+
article.is_published! # sets the published interaction to true
|
54
|
+
article.isnt_published! # sets the published interaction to false
|
55
|
+
article.is_published? #=> false
|
56
|
+
|
57
|
+
# you can also combine keys
|
58
|
+
article.is_published_and_not_promoted! #=> nil
|
59
|
+
article.is_published_and_promoted? #=> false
|
60
|
+
article.is_published_and_not_promoted? #=> true
|
61
|
+
|
62
|
+
# more complex values
|
63
|
+
article.set :tags, [:ruby, :rails, :gem] # or article.set "tags", [:ruby, :rails, :gem]
|
64
|
+
article.get :tags #=> ["ruby", "rails", "gem"]
|
65
|
+
article.has? :tags #=> true
|
66
|
+
article.set :tags, nil
|
67
|
+
article.has? :tags #=> false
|
68
|
+
```
|
69
|
+
|
70
|
+
### Class methods
|
71
|
+
|
72
|
+
You can query interactable objects in a similar fasion.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
# similar to instance methods there are simple lookup methods
|
76
|
+
Article.is_published #=> all articles for which published == true
|
77
|
+
Article.isnt_published #=> all articles for which published != true
|
78
|
+
|
79
|
+
# again you can combine interactions
|
80
|
+
Article.is_published_and_promoted #=> all promoted and published articles
|
81
|
+
```
|
82
|
+
|
83
|
+
## Interaction history
|
84
|
+
|
85
|
+
One of the advantages of Interactions is that they are kept as a permanent log. Thefefore you can easily look up if an interaction has ever happened in the past.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
# look up if an article has ever been published
|
89
|
+
article.was_published? #=> false
|
90
|
+
article.is_published! #=> nil
|
91
|
+
article.isnt_published! #=> nil
|
92
|
+
article.is_published? #=> false
|
93
|
+
article.was_published? #=> true
|
94
|
+
article.wasnt_published? #=> false
|
95
|
+
|
96
|
+
# again you can combine keys
|
97
|
+
article.was_published_and_promoted? #=> false
|
98
|
+
|
99
|
+
# to fully inspect the log you need to dig a bit deeper
|
100
|
+
# and access the interactions directly
|
101
|
+
article.history :published #=> all interactions that match this key
|
102
|
+
article.history "published"
|
103
|
+
article.history :published, :promoted
|
104
|
+
article.history ["published", "promoted"]
|
105
|
+
```
|
106
|
+
|
107
|
+
## Advanced usage
|
108
|
+
|
109
|
+
A lot of the magic methods have underlying dumb methods that can be used too to further customize your needs.
|
110
|
+
|
111
|
+
### Instance methods
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
article.is "published" #=> nil
|
115
|
+
article.is :published, :promoted #=> nil
|
116
|
+
article.is [:published, :not_promoted] #=> nil
|
117
|
+
article.is? :published, :promoted #=> false
|
118
|
+
article.is? [:published, :not_promoted] #=> true
|
119
|
+
article.get :promoted #=> true
|
120
|
+
```
|
121
|
+
|
122
|
+
### Class methods
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
Article.inter(published: true) # loads all articles for which the current published state us true
|
126
|
+
Article.inter(published: true).first # lazy loads the latest article
|
127
|
+
# You can also get access to the raw interactions
|
128
|
+
Article.interactions(published: true)
|
129
|
+
```
|
130
|
+
|
131
|
+
### Related lookup methods
|
132
|
+
|
133
|
+
Finally if 2 related models are both interactable then you can query related models by interaction status.
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
class Article < ActiveRecord::Base
|
137
|
+
include Inter::Actable
|
138
|
+
has_many :comments
|
139
|
+
end
|
140
|
+
|
141
|
+
class Comment < ActiveRecord::Base
|
142
|
+
include Inter::Actable
|
143
|
+
belongs_to :article
|
144
|
+
end
|
145
|
+
```
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
article.comments.first.is_spam! #=> nil
|
149
|
+
article.spam_comments #=> all comments with an interaction of "spam" set to true
|
150
|
+
article.not_spam_comments #=> all comments with an interaction of "spam" != true
|
151
|
+
article.spam_comments_ids #=> an array of IDs
|
152
|
+
article.spam_comments_count #=> the length of all the comments that match the criteria
|
153
|
+
|
154
|
+
# all of this maps down to the known pattern
|
155
|
+
article.comments.inter(spam: true)
|
156
|
+
```
|
157
|
+
|
158
|
+
## Changelog
|
159
|
+
|
160
|
+
* 0.0.2 Copied over implementation
|
161
|
+
* 0.0.1 Gem skeleton
|
162
|
+
|
163
|
+
## Contributors
|
164
|
+
|
165
|
+
* [Cristiano Betta](http://github.com/cbetta) - Developer Evangelist - PayPal
|
166
|
+
|
167
|
+
## Todos
|
168
|
+
|
169
|
+
* Add a generator
|
170
|
+
* Add tests
|
171
|
+
* Add option to disable history
|
172
|
+
|
173
|
+
## License
|
174
|
+
|
175
|
+
See [LICENSE](/LICENSE)
|
176
|
+
|
data/Rakefile
ADDED
data/inter.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require "inter/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "inter"
|
8
|
+
s.version = Inter::VERSION
|
9
|
+
s.authors = ["Cristiano Betta"]
|
10
|
+
s.email = ["cbetta@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/cbetta/inter"
|
12
|
+
s.summary = "Smart interactions on any ActiveRecord model"
|
13
|
+
s.description = "An easy and simple way to keep track of interactions on an ActiveRecord model that would normally require a lot of boolean attributes to make possible."
|
14
|
+
s.license = 'MIT License'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rails"
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "rspec-rails"
|
24
|
+
s.add_development_dependency "sqlite3"
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Inter
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../templates", __FILE__)
|
5
|
+
desc "Installs Inter migrations"
|
6
|
+
|
7
|
+
def copy_migrations
|
8
|
+
time = Time.now.strftime("%Y%m%d%H%M%S")
|
9
|
+
template "create_interactions.rb", "db/migrate/#{time}_create_interactions.rb"
|
10
|
+
|
11
|
+
puts "Migrations completed. Please run `rake db:migrate` to finish the instalation"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CreateInteractions < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :interactions do |t|
|
4
|
+
t.integer "interactable_id"
|
5
|
+
t.string "interactable_type"
|
6
|
+
t.string "key"
|
7
|
+
t.string "value"
|
8
|
+
t.datetime "created_at", null: false
|
9
|
+
t.datetime "updated_at", null: false
|
10
|
+
t.boolean "current", default: true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/inter.rb
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
module Inter
|
2
|
+
module Actable
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.has_many :interactions, class_name: Inter::Action, as: :interactable
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# def interaction key, value
|
10
|
+
# interactions(key, value).first
|
11
|
+
# end
|
12
|
+
|
13
|
+
# def interactions key, value
|
14
|
+
# Inter::Action.where(key: key, value: value.to_json, current: true, interactable_type: self.name)
|
15
|
+
# end
|
16
|
+
|
17
|
+
# def get *options
|
18
|
+
# interaction(key, value).try(:interactable)
|
19
|
+
# end
|
20
|
+
|
21
|
+
# def get_all key, value
|
22
|
+
# interactions(key, value).map{|int| int.interactable }
|
23
|
+
# end
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(meth, *args, &block)
|
27
|
+
case meth.to_s
|
28
|
+
when /^is_(.+)!$/
|
29
|
+
is *process($1)
|
30
|
+
when /^is(nt_.+)!$/
|
31
|
+
is *process($1)
|
32
|
+
when /^is_(.+)\?$/
|
33
|
+
is? *process($1)
|
34
|
+
when /^is(nt_.+)\?$/
|
35
|
+
is? *process($1)
|
36
|
+
when /^was_(.+)\?$/
|
37
|
+
was? *process($1)
|
38
|
+
when /^was(nt_.+)\?$/
|
39
|
+
was? *process($1)
|
40
|
+
else
|
41
|
+
super
|
42
|
+
end
|
43
|
+
# if meth.to_s =~ /^is_(.+)\?$/
|
44
|
+
# check_boolean_interactable($1)
|
45
|
+
# elsif meth.to_s =~ /^is_(.+)!$/
|
46
|
+
# is $1
|
47
|
+
# elsif meth.to_s =~ /^isnt_(.+)!$/
|
48
|
+
# isnt $1
|
49
|
+
# elsif meth.to_s =~ /^has_(.+)\?$/
|
50
|
+
# check_length_interactable($1)
|
51
|
+
# elsif meth.to_s =~ /^all_([a-z]+)_counts$/
|
52
|
+
# count_all_for $1
|
53
|
+
# elsif meth.to_s =~ /^([a-z_]+)_([a-z]+)_count$/
|
54
|
+
# count_combined_interaction_relations $1, $2
|
55
|
+
# elsif meth.to_s =~ /^([a-z_]+)_([a-z]+)_ids$/
|
56
|
+
# combined_interaction_relation_ids $1, $2
|
57
|
+
# elsif meth.to_s =~ /^([a-z_]+)_([a-z]+)$/
|
58
|
+
# combined_interaction_relations $1, $2
|
59
|
+
# else
|
60
|
+
# super
|
61
|
+
# end
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Booleans
|
66
|
+
#
|
67
|
+
def is *names
|
68
|
+
[names].flatten.each do |name|
|
69
|
+
set key_for(name), value_for(name)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def is? *names
|
74
|
+
[names].flatten.each do |name|
|
75
|
+
value = value_for(name)
|
76
|
+
values = value == false ? [value, nil] : [value]
|
77
|
+
return false unless values.include?(get(key_for(name)))
|
78
|
+
end
|
79
|
+
true
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# Complex values
|
84
|
+
#
|
85
|
+
def set key, value
|
86
|
+
interactions.create(key: key, value: value)
|
87
|
+
end
|
88
|
+
|
89
|
+
def get key
|
90
|
+
interactions.where(key: key, current: true).first.try(:value)
|
91
|
+
end
|
92
|
+
|
93
|
+
def has? key
|
94
|
+
get(key) != nil
|
95
|
+
end
|
96
|
+
|
97
|
+
#
|
98
|
+
# History
|
99
|
+
#
|
100
|
+
def was? *names
|
101
|
+
[names].flatten.each do |name|
|
102
|
+
if value_for(name)
|
103
|
+
return false if history(key_for(name)).count == 0
|
104
|
+
else
|
105
|
+
return false if history(key_for(name)).count > 0
|
106
|
+
end
|
107
|
+
end
|
108
|
+
true
|
109
|
+
end
|
110
|
+
|
111
|
+
def history *names
|
112
|
+
[names].flatten.map {|name| interactions.where(key: name)}.flatten
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def process options
|
118
|
+
options.split("_and_")
|
119
|
+
end
|
120
|
+
|
121
|
+
def value_for name
|
122
|
+
name = name.to_s
|
123
|
+
!(name.starts_with?("not_") || name.starts_with?("is_not_") || name.starts_with?("isnt_") || name.starts_with?("was_not_") || name.starts_with?("wasnt_") || name.starts_with?("nt_"))
|
124
|
+
end
|
125
|
+
|
126
|
+
def key_for name
|
127
|
+
name = name.to_s
|
128
|
+
name.gsub(/^(is_not_)|(not_)|(isnt_)|(nt_)|(is_)|(was_not_)|(wasnt_)|(was_)/, "")
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
# private
|
133
|
+
|
134
|
+
# def check_boolean_interactable key
|
135
|
+
# get(key) == true
|
136
|
+
# end
|
137
|
+
|
138
|
+
# def check_length_interactable key
|
139
|
+
# interactions.where(key: key).count > 0
|
140
|
+
# end
|
141
|
+
|
142
|
+
# def count_all_for relation
|
143
|
+
# relation_ids = send(relation).pluck(:id)
|
144
|
+
# results = Inter::Action.where(interactable_id: relation_ids, current: true.to_s, value: true.to_s).pluck(:key)
|
145
|
+
# results.inject(Hash.new(0)){|h,k| h[k] += 1; h}
|
146
|
+
# end
|
147
|
+
|
148
|
+
# def count_combined_interaction_relations interactions, relation
|
149
|
+
# combined_interaction_relation_ids(interactions, relation).count
|
150
|
+
# end
|
151
|
+
|
152
|
+
# def combined_interaction_relations interactions, relation
|
153
|
+
# ids = combined_interaction_relation_ids interactions, relation
|
154
|
+
# klass = relation.singularize.capitalize.constantize
|
155
|
+
# klass.find(ids)
|
156
|
+
# end
|
157
|
+
|
158
|
+
# def combined_interaction_relation_ids interactions, relation
|
159
|
+
# interactions = interactions.split("_and_")
|
160
|
+
# relation_ids = send(relation).pluck(:id)
|
161
|
+
# interactions.each do |interaction|
|
162
|
+
# value = true
|
163
|
+
# if interaction.starts_with? "not_"
|
164
|
+
# interaction = interaction.gsub("not_", "")
|
165
|
+
# value = false
|
166
|
+
# end
|
167
|
+
# relation_ids &= Inter::Action.search(interaction, value, relation, relation_ids).pluck(:interactable_id).uniq
|
168
|
+
# end
|
169
|
+
# relation_ids
|
170
|
+
# end
|
171
|
+
end
|
172
|
+
end
|