Created
September 18, 2010 15:46
-
-
Save dekart/585773 to your computer and use it in GitHub Desktop.
This file contains 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 AuthHelper | |
def if_admin | |
if user = current_user && user.active? && user.roles.include?(:admin) | |
yield | |
end | |
end | |
end |
This file contains 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
<% if user = current_user && user.active? && user.roles.include?(:admin) %> | |
This is the secret message for admins | |
<% end %> | |
Some content | |
<% if user = current_user && user.active? && user.roles.include?(:admin) %> | |
More information for admins only | |
<% end %> |
This file contains 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
<% if_admin do %> | |
This is the secret message for admins | |
<% end %> | |
Some content | |
<% if_admin do %> | |
More information for admins only | |
<% end %> |
This file contains 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 ResultHelper | |
def result_for(action_name, &block) | |
builder = Builder.new(self, action_name) | |
concat(builder.html(&block)) | |
end | |
class Builder | |
def initialize(template, action_name) | |
@template = template | |
@action_name = action_name | |
end | |
def html(&block) | |
"This is HTML code" | |
end | |
end | |
end |
This file contains 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 ResultHelper | |
def result_for(action_name, &block) | |
concat( | |
Builder.new(self, action_name).html(&block) | |
) | |
end | |
class Builder | |
attr_reader :template | |
delegate :capture, :to => :template | |
def initialize(template, action_name) | |
@template = template | |
@action_name = action_name | |
end | |
def html(&block) | |
"This is HTML code" | |
end | |
def message(css_class = nil, &block) | |
@message = capture(&block) | |
@message_class = css_class | |
end | |
def buttons(css_class = nil, &block) | |
@buttons = capture(&block) | |
@buttons_class = css_class | |
end | |
def additional(css_class = nil, link_text = "Additional Info...", &block) | |
@additional = capture(&block) | |
@additional_class = css_class | |
@additional_link_Text = link_text | |
end | |
end | |
end |
This file contains 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 ResultHelper | |
def result_for(action_name, &block) | |
concat( | |
Builder.new(self, action_name).html(&block) | |
) | |
end | |
class Builder | |
attr_reader :template | |
delegate :capture, :content_tag, :link_to, :javascript_tag, :request, :to => :template | |
def initialize(template, action_name) | |
@template = template | |
@action_name = action_name | |
end | |
def html(&block) | |
result = [ | |
content_tag(:div, @message, :class => "message #{@message_class}"), | |
content_tag(:div, @buttons, :class => "buttons #{@buttons_class}") | |
] | |
if @additional | |
result << content_tag(:div, @additional, | |
:class => "additional #{@additional_class}", | |
:style => "display: none" | |
) | |
result << link_to(@additional_link_text, | |
"$('#{block_id}').find('.additional, a.show').toggle()", | |
:class => :show | |
) | |
end | |
result << javascript_tag( | |
"$(document).trigger('result.#{ request.xhr? ? :ajax : :inline }');" | |
) | |
content_tag(:div, result.join(" ").html_safe, | |
:class => "result", | |
:id => block_id | |
) | |
end | |
def block_id | |
"result_for_#{@action_name}" | |
end | |
def message(css_class = nil, &block) | |
@message = capture(&block) | |
@message_class = css_class | |
end | |
def success(&block) | |
message(:success, &block) | |
end | |
def failure(&block) | |
message(:failure, &block) | |
end | |
def notice(&block) | |
message(:notice, &block) | |
end | |
def buttons(css_class = nil, &block) | |
@buttons = capture(&block) | |
@buttons_class = css_class | |
end | |
def additional(css_class = nil, link_text = "Additional Info...", &block) | |
@additional = capture(&block) | |
@additional_class = css_class | |
@additional_link_text = link_text | |
end | |
end | |
end |
This file contains 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 ResultHelper | |
def result_for(name, options = {}) | |
message = content_tag(:div, options[:message], :class => :message) | |
buttons = content_tag(:div, options[:buttons].join(' '), :class => :buttons) | |
if options[:additional] | |
additional = content_tag(:div, options[:additional], | |
:class => :additional, | |
:style => "display:none" | |
) | |
additional << link_to_function('Additonal Info...', | |
'$("#result_for_action").find(".additional, a.show").toggle()', | |
:class => :show | |
) | |
else | |
additional = '' | |
end | |
javascript = javascript_tag( | |
"$(document).trigger('result.#{ request.xhr? ? :ajax : :inline }');" | |
) | |
content_tag(:div, | |
[message, buttons, additional, javascript].join(" ").html_safe, | |
:class => :result, | |
:id => "result_for_#{name}" | |
) | |
end | |
end |
This file contains 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
<div id="result_for_action" class="result"> | |
<div class="message"> | |
<%= message_text.html_safe %> | |
</div> | |
<div class="buttons"> | |
<%= link_to("Some Action", some_action_path) %> | |
<%= link_to("Other Action", other_action_path) %> | |
</div> | |
<% if additional_info %> | |
<div class="additional" style="display:none;"> | |
<%= additional_info.html_safe %> | |
</div> | |
<%= link_to_function('Additonal Info...', | |
'$("#result_for_action").find(".additional, a.show").toggle()', | |
:class => :show | |
) %> | |
<% end %> | |
</div> | |
<% javascript_tag do %> | |
<% if request.xhr? %> | |
$(document).trigger('result.ajax'); | |
<% else %> | |
$(document).trigger('result.inline'); | |
<% end %> | |
<% end %> |
This file contains 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
<%= result_for(:action, | |
:message => message_text, | |
:buttons => [ | |
link_to("Some Action", some_action_path), | |
link_to("Other Action", other_action_path) | |
], | |
:additional => additional_info | |
) %> |
This file contains 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
<%= result_for(:action, | |
:message => message_text, | |
:message_class => :success, | |
:buttons => if current_user | |
[ | |
link_to("Some Action", some_action_path), | |
link_to("Other Action", other_action_path) | |
] | |
else | |
[ | |
link_to("Login", login_path) | |
] | |
end, | |
:buttons_class => (:unauthorize unless current_user), | |
:additional => additional_info + (some_text || ""), | |
:additional_link_text => "Additional Info and Instructions..." | |
) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment