ruby - Overriding a method from a gem with a condition in Rails 4 -
i using gritter gem displaying simple notifications in rails app. in controllers use gflash method later adds notification in view so:
gflash notification: "welcome awesome website!"
now problem making different views mobile, , need override this method located in gflash module. want looks this:
if browser mobile browser else use original gflash method in gritter::gflash end
so added initializer in config/initializers/custom_gritter.rb:
gritter::gflash.module_eval def gflash *args @@mobile_notifications == [] ? @@mobile_notifications.push(args[0][:notice]) : @@mobile_notifications = [args[0][:notice]] end def self.mobile_notifications @@mobile_notifications ||= [] end def self.reset_mobile_notifications @@mobile_notifications = nil end end
this allows me have access user notifications in controllers or views , works fine:
gritter::gflash.mobile_notifications
but still have make sure overrides if user has mobile device changed custom_gritter.rb file to:
gritter::gflash.module_eval alias_method :original_gflash, :gflash def gflash *args puts "override works" if browser.mobile? puts "detected mobile browser" @@mobile_notifications == [] ? @@mobile_notifications.push(args[0][:notice]) : @@mobile_notifications = [args[0][:notice]] else original_gflash end end def self.mobile_notifications @@mobile_notifications ||= [] end def self.reset_mobile_notifications @@mobile_notifications = nil end end
in terminal see "override works" don't see "detected mobile browser" means condition doesn't work. browser.mobile?
method provided browser gem. used is_mobile_request? method the mobylette gem have exact same issue.
why isn't working? module have access methods in other gems? bad approach solve problem ? suggestion/idea appreciated, in advance !
update: don't have error means considers browser.mobile?
false when i'm opening browser on mobile( chrome). confirm adding puts "not mobile browser"
after else statement, appeared in terminal.
any idea why browser.mobile?
returns true in controller , false in module?