Skip to content

Commit

Permalink
Correctly substitute holder compare value (stop assuming it is a Stri…
Browse files Browse the repository at this point in the history
…ng), add note to README.md about potential gotcha
  • Loading branch information
skipperguy12 committed Feb 26, 2017
1 parent 6e062c6 commit 2505462
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2,286 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
scram (0.1.0)
scram (0.1.1)
mongoid (~> 6.1)
rails (~> 5.0, >= 5.0.1)

Expand Down Expand Up @@ -161,4 +161,4 @@ DEPENDENCIES
scram!

BUNDLED WITH
1.14.4
1.14.5
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -160,8 +160,24 @@ builder = Scram::DSL::Builders::ComparatorBuilder.new do
end
Scram::DSL::Definitions.add_comparators(builder)
```

Now your targets can use `asdf` as a conditions key, and Scram will use your method of comparison to determine if something is true or not. In this case, `asdf` returns true regardless of the two objects being compared.

#### Gotchas
Having trouble trying to use a holder check on a relation? Easy fix! The issue you are experiencing is just that the holder's scram_compare_value will probably be an ObjectId of some sort, but if you are comparing it against the relation... you are trying to compare the current holder's ObjectId to a document. The fix to this is just defining a condition within the model with the user you are trying to compare, and returning the object id of that.

Example of the issue:
Lets say your `Post` model `belongs_to :user`. If you tried setting up a condition which checks something like this: `:equals => { :'user' => "*holder" }` it will never work because of the above description. To fix it, define a condition which returns an ObjectId.

```ruby
scram_define do
condition :owner do |post|
post.user.scram_compare_value # we could also have done post.user.id
end
end
```

Now update your Target to have the following condition: `:equals => { :'*owner' => "*holder" }`. Voila! It will all work now, because you are correctly comparing the right data types.

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
2 changes: 1 addition & 1 deletion app/models/scram/target.rb
Expand Up @@ -57,7 +57,7 @@ def can? holder, action, obj
attribute = begin obj.send(:"#{field}") rescue return :abstain end

# Special value substitutions
model_value.gsub! "*holder", holder.scram_compare_value if model_value.respond_to?(:gsub!)
(model_value = holder.scram_compare_value) if model_value == "*holder"

# Abstain if this target doesn't apply to obj in any of its attributes
return :abstain unless comparator.call(attribute, model_value)
Expand Down
2 changes: 1 addition & 1 deletion lib/scram/version.rb
@@ -1,3 +1,3 @@
module Scram
VERSION = '0.1.0'
VERSION = "0.1.1"
end

0 comments on commit 2505462

Please sign in to comment.