techiferous / tabulous

Easy tabbed navigation for Rails
Other
322 stars 35 forks source link

Multiple tabsets - different renderers #42

Closed koukou73gr closed 10 years ago

koukou73gr commented 10 years ago

Would it be possible to override the default renderer for a specific tabset? Would it be hard to implement?

koukou73gr commented 10 years ago

Oh well, this wasn't so hard :)

I am sorry for supplying only a diff, I tried to do a pull request but I already had a fork of your repo with some changes at 1.x I didn't want to loose and GitHub won't allow a second fork.

The changes can be basically summed at:

Anyway, here goes:

diff --git a/lib/tabulous/config.rb b/lib/tabulous/config.rb
index 74e5137..8e406dc 100644
--- a/lib/tabulous/config.rb
+++ b/lib/tabulous/config.rb
@@ -39,10 +39,17 @@ module Tabulous
       end

       def renderer=(val)
-        unless val.is_a?(String) || val.is_a?(Symbol)
-          raise ImproperValueError, "renderer must be a string or a symbol"
-        end
+        @renderer = 
+          if val.is_a?(Hash)
+            unless val[:default].present?
+              raise ImproperValueError, "you should always define a :default renderer"
+            end
+            val.each {|k,v| check_renderer(v)}
+            val
+          else
+            check_renderer(val)
+            {default: val}
+          end
       end

       def when_action_has_no_tab=(val)
@@ -52,6 +59,13 @@ module Tabulous
         @when_action_has_no_tab = val
       end

+      private 
+      def check_renderer(val)
+        unless val.is_a?(String) || val.is_a?(Symbol)
+          raise ImproperValueError, "renderer must be a string or a symbol or a hash"
+        end
+      end
+
     end
   end
 end
diff --git a/lib/tabulous/dsl/setup.rb b/lib/tabulous/dsl/setup.rb
index e0acc96..b7a4faa 100644
--- a/lib/tabulous/dsl/setup.rb
+++ b/lib/tabulous/dsl/setup.rb
@@ -18,6 +18,7 @@ module Tabulous

         def tabs(tabset_name = :default, &block)
           tabset = Dsl::Tabs.process(&block)
+          tabset.name = tabset_name
           Tabsets.add(tabset_name, tabset)
         end

diff --git a/lib/tabulous/rendering_coordinator.rb b/lib/tabulous/rendering_coordinator.rb
index 8b19d64..de4de0c 100644
--- a/lib/tabulous/rendering_coordinator.rb
+++ b/lib/tabulous/rendering_coordinator.rb
@@ -21,9 +21,10 @@ module Tabulous
       @view = view
       @tabset = tabset
       begin
-        @renderer = "Tabulous::#{Config.renderer.to_s.camelize}Renderer".constantize.new(tabset, view)
+        renderer = Config.renderer[tabset.name] || Config.renderer[:default]
+        @renderer = "Tabulous::#{renderer.to_s.camelize}Renderer".constantize.new(tabset, view)
       rescue NameError
-        @renderer = "#{Config.renderer.to_s.camelize}Renderer".constantize.new(tabset, view)
+        @renderer = "#{renderer.to_s.camelize}Renderer".constantize.new(tabset, view)
       end
     end

diff --git a/lib/tabulous/tabset.rb b/lib/tabulous/tabset.rb
index 94f6715..e9fd91c 100644
--- a/lib/tabulous/tabset.rb
+++ b/lib/tabulous/tabset.rb
@@ -1,6 +1,8 @@
 module Tabulous
   class Tabset

+    attr_accessor :name
+
     def initialize
       @tabs = []
     end
techiferous commented 10 years ago

Thanks for this idea. For simplicity's sake, I prefer to stick with just one renderer. My take would be to create a renderer that supplied markup that would work for both tabsets, and use CSS (or JS) to differentiate their appearance and behavior.