Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Last active March 28, 2016 12:03
Show Gist options
  • Save christiantakle/347614aac3cce50f929b to your computer and use it in GitHub Desktop.
Save christiantakle/347614aac3cce50f929b to your computer and use it in GitHub Desktop.
Functional style, instead of adding conditional logic in the template use the fact that [] is a monoid. [a] + [] = [a] and [a] + [b] = [a,b]
:ruby
getAttrs = ->(link) {{href: link.fetch(:href), class: link.fetch(:class)}}
# Added to prevent typos in `tab_to_conf` and `mkLink`
events, following, calendar, overview, manager, followers, widget =
%w(events following calendar overview manager followers widget)
tab_to_conf =
{ calendar => [t('users.profile.calendar'), user_path(user)],
following => [t('users.profile.following'), following_user_path(user)],
events => [t('shared.header.menu.my_events'), events_user_path(user)],
overview =>
[ t('user_account.events.overview_events_text'),
events_overview_user_path(user) ],
manager =>
[ t('event_manager.login.headline'),
events_manager_user_path(user)],
followers =>
[ t('users.profile.followers', count: followers_count),
followers_user_path(user)],
widget =>
[ t('user_account.events.embed_widget_events_button'),
events_website_integration_user_path(user)]
}
# Using Curry here, Just using a closure might be more clear
mkLink =
->(current_tab, tab) do
t, h = tab_to_conf.fetch(tab)
{title:t, href:h, class:('muted' if current_tab === tab)}
end
.curry
.(current_tab)
user_links =
is_current_user ?
[ mkLink.(events),
mkLink.(overview),
mkLink.(manager),
mkLink.(widget) ] : []
public_links =
[ mkLink.(calendar),
mkLink.(followers),
mkLink.(following) ]
links = user_links + public_links
.profile-navigation__links.text--center
- links.each do |l|
%a.profile-navigation__link.chamber--right{getAttrs.(l)}= l.fetch(:title)
@christiantakle
Copy link
Author

Pseudo example in Javascript

const
  common = // :: [Link]
    [home, about, status ...],

  user =  // :: [Link]
    isAdmin
      ? [settings]
      : [],

  private = // :: [Link]
    isAdmin
      ? [admin, ...]
      : [],

links = // :: [Link]
  common
    .concat(user)
    .concat(private)
  // common + user + private | common <> user <> private | common  `mappend` user `mappend` private

toLink :: Link -> Html
links.map(toLink) // :: Html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment