Created
March 9, 2012 20:09
-
-
Save nacengineer/2008389 to your computer and use it in GitHub Desktop.
Bitmask Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# model code | |
property :paths_mask, Integer | |
NODE_PATH = %w(path_1 path_2 path_3 path_4) | |
def paths=(paths) | |
self.paths_mask = (paths & NODE_PATH).map { |r| 2**NODE_PATH.index(r) }.sum | |
end | |
def paths | |
NODE_PATH.reject do |r| | |
((paths_mask || 0) & 2**NODE_PATH.index(r)).zero? | |
end | |
end | |
# not using is_a? here because Devise doesn't like that | |
def is_an?(path) | |
paths.include?(path.to_s) | |
end | |
# view code in haml | |
%h2 Paths this Question Applies to | |
- for path in Question::NODE_PATH | |
%p | |
= check_box_tag "question[paths][]", path, @question.paths.include?(path) | |
= path.humanize.titleize | |
= hidden_field_tag "question[paths][]", "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment