excid3 / simple_calendar

A wonderfully simple calendar gem for Rails
http://excid3.github.io/simple_calendar
MIT License
1.57k stars 267 forks source link

Any interest in a HAML generator? #338

Closed pmichaeljones closed 6 months ago

pmichaeljones commented 7 months ago

Hi Chris, With Haml 6, the old "hack" for the calendar isn't working anymore. HAML 6 removed a lot of methods, one of which is block_is_haml? and capture_haml.

<% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(passed_block) %>
    <% capture_haml(day, sorted_events.fetch(day, []), &passed_block) %>
<% else %>
    <% passed_block.call day, sorted_events.fetch(day, []) %>
<% end %>

This HAML code works for calendar layouts. You just need to make sure you're using HAML blocks, but if someone has HAML installed, they're most likely using it for all templates.

/ The capture block below only works with HAML templates
.simple-calendar
  = link_to "Previous", start_date: date_range.first - 1.day  
  = I18n.t("date.month_names")[start_date.month] 
  = start_date.year
  = link_to "Next", start_date: date_range.last + 1.day

  %table{class: 'table table-striped'}
    %thead
      %tr
        - date_range.slice(0,7).each do |day|
          %th
            = I18n.t("date.abbr_day_names")[day.wday] 

    %tbody
      - date_range.each_slice(7) do |week|
        %tr
          - week.each do |day|
            = content_tag :td, class: calendar.td_classes_for(day) do 
              - capture(day, sorted_events.fetch(day, []), &passed_block)

If I were to add a HAML generator to the gem, is that something you would be interested in?

excid3 commented 7 months ago

Does Haml 6 work with the regular Rails capture now?

If so, we could just check if it responds_to the Haml methods instead. I'd rather not maintain another generator and template if we can avoid it.

pmichaeljones commented 7 months ago

I believe so. I've been testing out all my templates with capture instead of capture_haml and they appear to be working as expected.

<tbody>
  <% date_range.each_slice(7) do |week| %>
    <tr>
      <% week.each do |day| %>
        <%= content_tag :td, class: calendar.td_classes_for(day) do %>
          <% capture(day, sorted_events.fetch(day, []), &passed_block) %>
        <% end %>
      <% end %>
    </tr>
  <% end %>
</tbody>

All my templates are HAML, so I pulled out the block_is_haml? and responds_to?(Haml) pieces of code.

I'm still on Haml 5 and it's working. Haml6 pulls out those methods (capture_haml, block_is_haml?, etc)

pmichaeljones commented 7 months ago

The Haml5 to Haml6 upgrade notes specifically mention using Rail's capture method instead of capture_haml

Screen Shot 2024-03-19 at 6 47 52 PM
excid3 commented 7 months ago

We removed the Haml specific code back in this commit actually: https://github.com/excid3/simple_calendar/commit/9c9c8405df4232c42a372b409bc518766756a186

I think this removed the need for the haml_capture.

pmichaeljones commented 7 months ago

Ah okay. I was unaware of the instance_exec method. I'll give it a try tomorrow. Thanks for a great gem Chris. You're a real GEM of the rails community. horrible pun!