maglevhq / maglev-core

Ruby on Rails website builder
https://www.maglev.dev
MIT License
273 stars 47 forks source link

fix(json): page sections not camelized #50

Closed hasaniskandar closed 1 year ago

hasaniskandar commented 1 year ago

Fixes #47, fixes #48.

did commented 1 year ago

Hmm, there is a good reason why I put the line at the top of the file but I can't remember. I'm going to investigate it...

hasaniskandar commented 1 year ago

Noted.

pgruener commented 1 year ago

There's another problem, which is solved by the PR: When you open the Navigation (of the std provided navbar) the tree isn't rendered hierarchically well (initially) image

You'll have to persist the page (each time), before you'll have this rendered right, displayed here: image

I am not really sure, if there might be side-effects, as I noticed that anytime all the properties are camelized when they're load from the editor (but not initially when app/views/maglev/editor/show.html.erb is rendered):

image

I now used a really dirty workaround in a monkey patch to get these problems sorted out without adding this PR, but in the meantime I'd expect the original proposal from @hasaniskandar might work reliably ...?

def camelcase_relevant_props(obj, props)
  case obj
  when Array
    obj.map { |item| camelcase_relevant_props(item, props) }
  when Hash
    Hash[obj.map { |key, value|
      key_was_sym = key.is_a?(Symbol)

      if props.include?(key.to_sym)
        camelized_key = key.to_s.camelize(:lower)
        [key_was_sym ? camelized_key.to_sym : camelized_key, camelcase_relevant_props(value, props)]
      else
        [key, camelcase_relevant_props(value, props)]
      end
    }]
  else
    obj
  end
end

camelize_page_props = %i[parent_id link_label link_id link_type section_id open_new_window alt_text].freeze

json.sections camelcase_relevant_props(services.get_page_sections.call(page: page), camelize_page_props)
did commented 1 year ago

thanks a lot @hasaniskandar and also @pgruener for your help!