My first plugin. Need advice


(Steven Slade) #1

I am creating my first plugin. Here’s the outline on what it should do:

It should allow me to get the custom_groups of a user. The reason there is some handlebars in the plugin is because I eventually want to change the state of nav-bar depending on a user’s custom_groups.

The plugin should access the Users Controller, get their custom groups with:
groups = current_user.groups.all.select("name") and then take the data from that and render it in json for the javascripts/initializer to make an ajax call and get the user’s custom groups.

Here is the plugin: GitHub - stevenpslade/discourse-data-menu-item

As aforementioned, this is my first one and I am having trouble even testing what results I’m getting from the ajax call as well as if the code in the controller is correct. I’m used to being in a pure rails app where I can use byebug’s and binding.pry’s.

If a more experienced plugin dev could give this a once over and maybe give me some advice, I would really appreciate that. I hope to continue making Discourse plugins after I get past this first one! :slightly_smiling:


(Régis Hanol) #2

Here’s my take on it

  • Rather than creating a new controller, I would instead extend the CurrentUserSerializer and add the groups
  • Then I would use one of the extensibility point for the navigation bar template (and controller) to add my own logic depending on the groups returned

(Steven Slade) #3

@zogstrip: In the CurrentUserSerializer, would it need something like:

def group_names
    object.groups.all
  end 

I am playing around with it and trying to test in dev tools using Discourse.User.current().group_names but it’s coming up undefined. Obviously object.groups.all won’t do the trick here, what would I need to do to add the groups?


(Sam Saffron) #4

Group names is object.groups.pluck(:name)


(James Kiesel) #5

Chrome (other modern browsers too) have quite fantastic javascript debuggers built into them, which should provide you with all the breakpointy goodness you’d be used to from ruby.

There’s a great codeschool tutorial on it as well:
http://discover-devtools.codeschool.com/