Hiccdown Development Notes

Discussion started by Dennis Hackethal

  Log in or sign up to participate in this discussion.
With an account, you can revise, criticize, and comment on ideas, and submit new ideas.

Notes about developing the Ruby gem Hiccdown.


Discussions can branch out indefinitely. Zoom out for the bird’s-eye view.
Dennis Hackethal’s avatar
Dennis HackethalOP revised 12 days ago·#1978

Hiccdown methods should live in Rails helpers as instance methods.

CriticismCriticized1oustanding criticism
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#301

That isn’t a good idea because Hiccdown methods often share the same conventional names (index, show, etc), which can and does lead to conflict.

Criticism of #1978
Dennis Hackethal’s avatar
Dennis HackethalOP revised 12 days ago·#1980

Hiccdown methods should live in Rails helpers as class methods. That way, the problem described in #302 is solved – methods can be referenced unambiguously:

ProductsHelper.index
StoresHelper.index
CriticismCriticized2oustanding criticisms
Dennis Hackethal’s avatar
Dennis HackethalOP revised about 1 year ago·#305

Does that mean they wouldn’t have access to the view_context? If so, calling helper methods from inside these class methods wouldn’t be possible.

Criticism of #1980Criticized1oustanding criticism
Dennis Hackethal’s avatar
Dennis HackethalOP revised about 1 year ago·#310

If so, there might be a way to bind them to the view_context. Or I could definitely pass the view_context explicitly as the first parameter:

So instead of

@helper_module.instance_method(@action_name).bind_call(view_context)

I would do

@helper_module.send(@action_name, view_context)

And the parameter list of each Hiccdown method would start accordingly:

module ProductsHelper
  def self.index vc #, …
    vc.some_helper_method
  end

  def some_helper_method
    # …
  end
end
Criticism of #305
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#312

That would be mixing class methods an instance methods in Rails helper modules, which typically only contain instance methods. Not idiomatic Rails usage.

Criticism of #1980
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#332

#327 applies here, too: no access to instance variables inside helper class methods.

Criticism of #1980
Dennis Hackethal’s avatar
Dennis HackethalOP revised 12 days ago·#1982

Hiccdown methods should live in their own, separate classes. How about they are called ‘displays’?

class ProductsDisplay
  def index vc, # …
    vc.some_helper_method
  end
end

Behind the scenes, the Hiccdown gem would need to make the instance variables available to the display class:

display = @display_module.new

view_context.instance_variables.each do |iv|
  display.instance_variable_set(
    iv,
    view_context.instance_variable_get(iv)
  )
end

Then:

class ProductsDisplay
  def index vc, # …
    vc.some_helper_method(@products)
  end
end
CriticismCriticized1oustanding criticism
Dennis Hackethal’s avatar
Dennis HackethalOP revised about 1 year ago·#317

Then how would you call index from a helper method?

Criticism of #1982Criticized1oustanding criticism
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#315

I don’t think that’s something people would do a lot, but they still easily could: ProductsRenderer.index(self)

Criticism of #317
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#321

Test this!

Criticism of #315Criticized1oustanding criticism
Dennis Hackethal’s avatar
Dennis HackethalOP revised about 1 year ago·#323

Tested, it works. self does indeed point to the view_context in the helper. Verified by printing object_ids.

Criticism of #321
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#331

Not as of #330, they couldn’t.

Criticism of #315Criticized1oustanding criticism
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#341

It doesn’t really matter. This would be like calling a controller action from a helper method. Not something people do.

Criticism of #331
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#326

I’m trying this now. Having to prepend every invocation of a helper method with vc. is getting really old really fast.

Criticism of #1982
Dennis Hackethal’s avatar
Dennis HackethalOP, about 1 year ago·#333

Having explored three different ideas, I believe #302 – having regular helper methods to render Hiccdown structures – is the best.

The idea is not without its flaws, but having to qualify a method name by, say, calling it idea_form instead of form is still better than manually having to pass the view context around all the time and not being able to trivially access instance variables.

So I’ll stick with #302 for now, which is the status quo already.

Dennis Hackethal’s avatar
Dennis HackethalOP revised about 1 year ago·#335

I think the thing I’m really fighting here is Rails being object-oriented. Which I can’t do anything about.

Not sure the Rails team realizes how much OOP reduces the extensibility of Rails.

Dennis Hackethal’s avatar
Dennis HackethalOP, 12 months ago·#859

Could the errors around layouts be related to this?

Criticism
Dennis Hackethal’s avatar
Dennis HackethalOP, 12 days ago·#1984

Hiccdown should have support for ids and class names in the tag symbol. Like Hiccup.

[:'div#my-id.my-class.another-class']
# => <div id="my-id" class="my-class another-class"></div>

It should also allow mixing:

[:'div#my-id.my-class.another-class', {id: 'override', class: 'additive'}]
# => <div id="override" class="my-class another-class additive"></div>

In other words, the id from the hash would override the id from the symbol, and the class from the hash would be added to the classes from the symbol.

Criticism