Extract BodyClass #39

Closed
croaky opened this Issue Dec 1, 2012 · 4 comments

Projects

None yet

3 participants

@croaky
Member
croaky commented Dec 1, 2012

I'm thinking the body_class helper doesn't belong in Flutie and could be re-written to be more readable. Suggested implementation below.

Potential places it could be moved:

  • Suspenders
  • Its own body_class gem
# Usage:
#
# <body class='<%= body_class %>'>
# <body class='users users-index'>
#
# <body class='<%= body_class(append: 'swipeable') %>'>
# <body class='users users-index swipeable'>
#
# <body class='<%= body_class %>'>
# content_for(append_to_body_class: 'admin')
# <body class='users users-index admin'>

module BodyClassHelper
  def body_class
    BodyClass.new(options).to_s
  end

  class BodyClass
    def initialize(options = {})
      @options = options
    end

    def to_s
      [
        controller_name,
        namespaced_action_name,
        appended_from_options,
        appended_from_view_template
      ].join(' ')
    end

    private

    def controller_name
      @controller_name ||= controller.controller_path.gsub('/', '-')
    end

    def namespaced_action_name
      "#{controller_name}-#{controller.action_name}"
    end

    def appended_from_options
      @options[:append]
    end

    def appended_from_view_template
      if content_for?(:append_to_body_class)
        content_for(:append_to_body_class)
      end
    end
  end
end
@calebthompson
Contributor

Do you think that Flutie's new focus on providing view helpers satisfies the concern about whether body_class belongs here?

@calebthompson
Contributor

Additionally, what is the purpose of having both :append and :append_to_body_class options?

@croaky
Member
croaky commented Feb 21, 2013

Agree it should stay in Flutie.

Don't remember what I was doing with an alternate version here. May have just been refactoring and experimenting with having most of the work being done in a class that could be instantiated and tested directly.

The append_to_body_class option is meant to be used from content_for. It needs the more verbose version to make sense in that context.

@jferris
Member
jferris commented Feb 21, 2013

I'm closing, since we want to keep body_class in flutie, and any refactoring or new features can be submitted as pull requests.

@jferris jferris closed this Feb 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment