Created
January 7, 2011 19:12
-
-
Save javierv/769950 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
From 4525b92fa2bf6526fc6c5b5b4b178698eabde537 Mon Sep 17 00:00:00 2001 | |
From: =?UTF-8?q?Javier=20Mart=C3=ADn?= <elretirao@elretirao.net> | |
Date: Fri, 7 Jan 2011 19:52:44 +0100 | |
Subject: [PATCH 1/3] Refactor links to actions. | |
--- | |
lib/tabletastic/table_builder.rb | 14 +++++++++----- | |
1 files changed, 9 insertions(+), 5 deletions(-) | |
diff --git a/lib/tabletastic/table_builder.rb b/lib/tabletastic/table_builder.rb | |
index b9ebe7f..72df84e 100644 | |
--- a/lib/tabletastic/table_builder.rb | |
+++ b/lib/tabletastic/table_builder.rb | |
@@ -97,17 +97,23 @@ module Tabletastic | |
actions = [actions] if !actions.respond_to?(:each) | |
actions = [:show, :edit, :destroy] if actions == [:all] | |
actions.each do |action| | |
- action_link(action.to_sym, prefix) | |
+ action_cell(action.to_sym, prefix) | |
end | |
end | |
# Dynamically builds links for the action | |
- def action_link(action, prefix) | |
+ def action_cell(action, prefix) | |
html_class = "actions #{action.to_s}_link" | |
block = lambda do |resource| | |
compound_resource = [prefix, resource].compact | |
compound_resource.flatten! if prefix.kind_of?(Array) | |
- case action | |
+ action_link(action, compound_resource) | |
+ end | |
+ self.cell(action, :heading => "", :cell_html => {:class => html_class}, &block) | |
+ end | |
+ | |
+ def action_link(action, compound_resource) | |
+ case action | |
when :show | |
@template.link_to(link_title(action), compound_resource) | |
when :destroy | |
@@ -117,8 +123,6 @@ module Tabletastic | |
@template.link_to(link_title(action), | |
@template.polymorphic_path(compound_resource, :action => action)) | |
end | |
- end | |
- self.cell(action, :heading => "", :cell_html => {:class => html_class}, &block) | |
end | |
protected | |
-- | |
1.7.1 |
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
From 6ad861ba441c613388e1989abb0c1d4848222771 Mon Sep 17 00:00:00 2001 | |
From: =?UTF-8?q?Javier=20Mart=C3=ADn?= <elretirao@elretirao.net> | |
Date: Fri, 7 Jan 2011 20:03:00 +0100 | |
Subject: [PATCH 2/3] Extract action link method into the helper. | |
That method used @template a lot, so it makes sense to move it to the helper. | |
--- | |
lib/tabletastic/helper.rb | 21 +++++++++++++++++++++ | |
lib/tabletastic/table_builder.rb | 26 ++------------------------ | |
2 files changed, 23 insertions(+), 24 deletions(-) | |
diff --git a/lib/tabletastic/helper.rb b/lib/tabletastic/helper.rb | |
index 7bafa53..594f687 100644 | |
--- a/lib/tabletastic/helper.rb | |
+++ b/lib/tabletastic/helper.rb | |
@@ -10,6 +10,19 @@ module Tabletastic | |
content_tag(:table, result, options[:html]) | |
end | |
+ def action_link(action, compound_resource) | |
+ case action | |
+ when :show | |
+ link_to(link_title(action), compound_resource) | |
+ when :destroy | |
+ link_to(link_title(action), compound_resource, | |
+ :method => :delete, :confirm => confirmation_message) | |
+ else # edit, other resource GET actions | |
+ link_to(link_title(action), | |
+ polymorphic_path(compound_resource, :action => action)) | |
+ end | |
+ end | |
+ | |
private | |
# Finds the class representing the objects within the collection | |
def default_class_for(collection) | |
@@ -29,6 +42,14 @@ module Tabletastic | |
def get_id_for(klass) | |
klass ? klass.model_name.collection : "" | |
end | |
+ | |
+ def link_title(action) | |
+ I18n.translate(action, :scope => "tabletastic.actions", :default => action.to_s.titleize) | |
+ end | |
+ | |
+ def confirmation_message | |
+ I18n.t("tabletastic.actions.confirmation", :default => "Are you sure?") | |
+ end | |
end | |
end | |
diff --git a/lib/tabletastic/table_builder.rb b/lib/tabletastic/table_builder.rb | |
index 72df84e..ce2eeeb 100644 | |
--- a/lib/tabletastic/table_builder.rb | |
+++ b/lib/tabletastic/table_builder.rb | |
@@ -2,8 +2,7 @@ require File.join(File.dirname(__FILE__), 'table_field') | |
module Tabletastic | |
class TableBuilder | |
- @@default_hidden_columns = %w[created_at updated_at created_on updated_on lock_version version] | |
- @@destroy_confirm_message = "Are you sure?" | |
+ @@default_hidden_columns = %w[created_at updated_at created_on updated_on lock_version version] | |
attr_reader :collection, :klass, :table_fields | |
@@ -107,24 +106,11 @@ module Tabletastic | |
block = lambda do |resource| | |
compound_resource = [prefix, resource].compact | |
compound_resource.flatten! if prefix.kind_of?(Array) | |
- action_link(action, compound_resource) | |
+ @template.action_link(action, compound_resource) | |
end | |
self.cell(action, :heading => "", :cell_html => {:class => html_class}, &block) | |
end | |
- def action_link(action, compound_resource) | |
- case action | |
- when :show | |
- @template.link_to(link_title(action), compound_resource) | |
- when :destroy | |
- @template.link_to(link_title(action), compound_resource, | |
- :method => :delete, :confirm => confirmation_message) | |
- else # edit, other resource GET actions | |
- @template.link_to(link_title(action), | |
- @template.polymorphic_path(compound_resource, :action => action)) | |
- end | |
- end | |
- | |
protected | |
def orm_fields | |
@@ -158,16 +144,8 @@ module Tabletastic | |
associations | |
end | |
- def confirmation_message | |
- I18n.t("tabletastic.actions.confirmation", :default => @@destroy_confirm_message) | |
- end | |
- | |
def content_tag(name, content = nil, options = nil, escape = true, &block) | |
@template.content_tag(name, content, options, escape, &block) | |
end | |
- | |
- def link_title(action) | |
- I18n.translate(action, :scope => "tabletastic.actions", :default => action.to_s.titleize) | |
- end | |
end | |
end | |
-- | |
1.7.1 |
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
From 0478e1c25892c2862779ce646485d301b5175efe Mon Sep 17 00:00:00 2001 | |
From: =?UTF-8?q?Javier=20Mart=C3=ADn?= <elretirao@elretirao.net> | |
Date: Fri, 7 Jan 2011 20:05:22 +0100 | |
Subject: [PATCH 3/3] Added alias for compatibility with previous versions. | |
--- | |
lib/tabletastic/table_builder.rb | 2 ++ | |
1 files changed, 2 insertions(+), 0 deletions(-) | |
diff --git a/lib/tabletastic/table_builder.rb b/lib/tabletastic/table_builder.rb | |
index ce2eeeb..eaf6254 100644 | |
--- a/lib/tabletastic/table_builder.rb | |
+++ b/lib/tabletastic/table_builder.rb | |
@@ -111,6 +111,8 @@ module Tabletastic | |
self.cell(action, :heading => "", :cell_html => {:class => html_class}, &block) | |
end | |
+ alias :action_link :action_cell | |
+ | |
protected | |
def orm_fields | |
-- | |
1.7.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment