Badgeable
Badgeable copied to clipboard
Add more attributes dynamically in the DSL
Is there a way to declare more attributes dynamically in the DSL? Im using badgeable_active_record I added the field to the migration but if I declare it in the DSL I get the following error:
badgeable-0.5.1/lib/badgeable/railtie.rb:18:in class_eval': undefined local variable or methodpoints' for #Badgeable::Config:0x007ff82f583a70 (NameError)
I tried adding the method by extending the class locally I created /lib/badgeable/config.rb where I defined the points method but i got the following error:
badgeable-0.5.1/lib/badgeable/railtie.rb:10:in block in <class:Railtie>': undefined methodbadge_definitions=' for Badgeable::Config:Class (NoMethodError)
Is there any option for me to be able to add attributes beside the default (name, icon, description)?
I could manage to get the points attribute working by adding an initializer that adds this method to the /lib/badgeable/config.rb class, but the points attrs is not being loaded in lib/badgeable/award.rb :
attrs = [:description, :icon].inject({}) {|hash, key| hash.merge(key => config.send(key)) }
I think you're on the right path, but the file you've created preceeded Badgeable's config.rb in the load paths. That's why you're getting undefined method badge_definitions=.
You probably want to reopen the Badgeable::Config class (perhaps in an initializer) and add a points method. I'm guessing something like this would work:
# config/initializers/badgeable_config_monkey.rb
module Badgeable
class Config
def points *args
if args.length === 1
@points = args[0]
else
@points
end
end
end
end
Load order of initializers may be an issue, since Badgeable::Railtie defines an initializer that reads your badge definitions. If this turns out to be a problem, let me know and I'll change it to allow monkey patching.
@jcroca take a look at #3 and see if it'll do what you are looking for.
yes this is exactly what I wanted thanks ill test it out and let u know
the fix worked really well but it seems there is a bug when using icon, Badge id: 16, name: "Explorer", description: nil, icon: "badges/explorer.png", points: 100, thumb: "badges/explorer-small.png"
u.badges.first.icon => "/images/explorer.jpg"
Although icon is defined it still returns the standard icon string
@jcroca try https://github.com/scottburton11/Badgeable/pull/3 (pull #3) again, I restored the original behavior for icon and description, which should fix the icon bug in the short term.