
<% = f.check_box: send_mail_on_friend_request%>
class User serialize: options, Hash end
class User
OPTION_1 =: key_1
OPTION_2 =: key_2
serialize: options, Hash
def option_1 = (value)
self.options [: key_1] = value
end
def option_2 = (value)
self.options [: key_2] = value
end
def option_1
self.options [: key_1]
end
def option_2
self.options [: key_2]
end
end
class User <ActiveRecord :: Base
NOTIFY_ON_MAIL = 0b0001
NOTIFY_ON_WALL_POST = 0b0010
NOTIFY_ON_FRIEND_REQUEST = 0b0100
NOTIFY_ON_COMMENT_ANSWER = 0b1000
def notify_on_mail
self.options & NOTIFY_ON_MAIL! = 0
end
def notify_on_mail = (value)
if value! = "0"
self.options | = const
else
self.options & = ~ const
end
end
end
class User <ActiveRecord :: Base
NOTIFY_ON_MAIL = 0b0001
NOTIFY_ON_WALL_POST = 0b0010
NOTIFY_ON_FRIEND_REQUEST = 0b0100
NOTIFY_ON_COMMENT_ANSWER = 0b1000
[NOTIFY_ON_MAIL, NOTIFY_ON_WALL_POST, NOTIFY_ON_FRIEND_REQUEST, NOTIFY_ON_COMMENT_ANSWER] .each do | notifer |
const = self.const_get (notifer)
define_method notifer.downcase.to_sym do
(mail_options & const)! = 0
end
define_method "# {notifer.downcase} =". to_sym do | flag |
if flag! = "0"
self.mail_options | = const
else
self.mail_options & = ~ const
end
end
end
end
class User <ActiveRecord :: Base
NOTIFY_ON_MAIL = 0b0001
NOTIFY_ON_WALL_POST = 0b0010
NOTIFY_ON_FRIEND_REQUEST = 0b0100
NOTIFY_ON_COMMENT_ANSWER = 0b1000
constants.grep (/ ^ NOTIFY _ /). each do | notifer |
const = self.const_get (notifer)
define_method notifer.downcase.to_sym do
(mail_options & const)! = 0
end
define_method "# {notifer.downcase} =". to_sym do | flag |
if flag! = "0"
self.mail_options | = const
else
self.mail_options & = ~ const
end
end
end
end
Source: https://habr.com/ru/post/95935/
All Articles