Badgeable icon indicating copy to clipboard operation
Badgeable copied to clipboard

Add more attributes dynamically in the DSL

Open juancroca opened this issue 13 years ago • 6 comments

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)?

juancroca avatar May 08 '12 18:05 juancroca

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)) }

juancroca avatar May 08 '12 20:05 juancroca

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.

scottburton11 avatar May 08 '12 20:05 scottburton11

@jcroca take a look at #3 and see if it'll do what you are looking for.

scottburton11 avatar May 09 '12 03:05 scottburton11

yes this is exactly what I wanted thanks ill test it out and let u know

juancroca avatar May 10 '12 00:05 juancroca

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

juancroca avatar May 12 '12 22:05 juancroca

@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.

scottburton11 avatar May 13 '12 21:05 scottburton11