Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ruby styleguide wrapper
module Styleguide
class BaseTag < Mustache
def initialize(parameters={})
@parameters = parameters
end
def self.base_path
"#{Styleguide::Engine.root}/app/assets/mustache/ui_component"
end
end
module Button
class Primary < BaseTag
self.template_file = "#{self.base_path}/button/primary.mustache"
def text
@parameters[:text]
end
def tag
if is_a_link?
return 'a'
end
'button'
end
def href
if is_a_link?
"href=#{@parameters[:href]}"
end
end
def full_width
if @parameters[:full_width]
"btn-block"
end
end
private
def is_a_link?
@parameters[:link]
end
end
class Favourite < BaseTag
self.template_file = "#{self.base_path}/button/favourite.mustache"
%w[data-love-it-button data-fav-object-id data-fav-url data-fav-image-url].each do |attribute|
eval <<-DEF
def #{attribute.underscore}
if @parameters['#{attribute}']
"#{attribute}=" + @parameters['#{attribute}'].to_s
end
end
DEF
end
end
class Favourited < Favourite
self.template_file = "#{self.base_path}/button/favourited.mustache"
end
class FavouriteNoText < Favourite
self.template_file = "#{self.base_path}/button/favourite_no_text.mustache"
end
class FavouritedNoText < Favourite
self.template_file = "#{self.base_path}/button/favourited_no_text.mustache"
end
end
module Link
class Regular < BaseTag
self.template_file = "#{self.base_path}/link/regular.mustache"
def url
@parameters[:url]
end
def text
@parameters[:text]
end
end
class MoreLessContent < BaseTag
self.template_file = "#{self.base_path}/link/more_less_content.mustache"
def content
@parameters[:content]
end
end
end
module UiComponentHelper
def ui_component(component_name, parameters={})
fully_qualified_class_name = 'styleguide/' + component_name
component = fully_qualified_class_name.classify.constantize.new(parameters)
component.render.html_safe
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment