Last active
August 29, 2015 14:13
-
-
Save agenteo/8f6e7c2b207652372aa9 to your computer and use it in GitHub Desktop.
ruby styleguide wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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