activeadmin 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activeadmin might be problematic. Click here for more details.

Files changed (255) hide show
  1. data/CHANGELOG.rdoc +15 -1
  2. data/Gemfile +41 -11
  3. data/LICENSE +5 -0
  4. data/README.rdoc +202 -87
  5. data/Rakefile +51 -15
  6. data/activeadmin.gemspec +310 -131
  7. data/cucumber.yml +1 -0
  8. data/features/comments/commenting.feature +81 -0
  9. data/features/comments/viewing_index.feature +19 -0
  10. data/features/edit_page.feature +77 -0
  11. data/features/first_boot.feature +16 -0
  12. data/features/global_navigation.feature +30 -0
  13. data/features/index/formats.feature +10 -0
  14. data/features/index/index_as_block.feature +15 -0
  15. data/features/index/index_as_blog.feature +50 -0
  16. data/features/index/index_as_grid.feature +45 -0
  17. data/features/index/index_as_table.feature +79 -0
  18. data/features/index/index_scopes.feature +52 -0
  19. data/features/index/pagination.feature +23 -0
  20. data/features/new_page.feature +75 -0
  21. data/features/registering_assets.feature +35 -0
  22. data/features/registering_resources.feature +33 -0
  23. data/features/show/default_content.feature +43 -0
  24. data/features/show/page_title.feature +33 -0
  25. data/features/sidebar_sections.feature +128 -0
  26. data/features/specifying_actions.feature +16 -0
  27. data/features/step_definitions/action_item_steps.rb +7 -0
  28. data/features/step_definitions/additional_web_steps.rb +67 -0
  29. data/features/step_definitions/asset_steps.rb +7 -0
  30. data/features/step_definitions/attribute_steps.rb +15 -0
  31. data/features/step_definitions/comment_steps.rb +8 -0
  32. data/features/step_definitions/configuration_steps.rb +31 -0
  33. data/features/step_definitions/factory_steps.rb +28 -0
  34. data/features/step_definitions/flash_steps.rb +3 -0
  35. data/features/step_definitions/format_steps.rb +7 -0
  36. data/features/step_definitions/index_scope_steps.rb +20 -0
  37. data/features/step_definitions/pagination_steps.rb +8 -0
  38. data/features/step_definitions/sidebar_steps.rb +11 -0
  39. data/features/step_definitions/tab_steps.rb +3 -0
  40. data/features/step_definitions/user_steps.rb +20 -0
  41. data/features/step_definitions/web_steps.rb +219 -0
  42. data/features/support/env.rb +88 -0
  43. data/features/support/paths.rb +48 -0
  44. data/features/users/logging_in.feature +34 -0
  45. data/features/users/logging_out.feature +13 -0
  46. data/lib/active_admin.rb +155 -79
  47. data/lib/active_admin/abstract_view_factory.rb +95 -0
  48. data/lib/active_admin/action_items.rb +4 -15
  49. data/lib/active_admin/arbre.rb +23 -0
  50. data/lib/active_admin/arbre/attributes.rb +20 -0
  51. data/lib/active_admin/arbre/class_list.rb +28 -0
  52. data/lib/active_admin/arbre/collection.rb +27 -0
  53. data/lib/active_admin/arbre/context.rb +8 -0
  54. data/lib/active_admin/arbre/core_extensions.rb +5 -0
  55. data/lib/active_admin/arbre/document.rb +42 -0
  56. data/lib/active_admin/arbre/element.rb +151 -0
  57. data/lib/active_admin/arbre/html.rb +84 -0
  58. data/lib/active_admin/arbre/html5_elements.rb +47 -0
  59. data/lib/active_admin/arbre/tag.rb +129 -0
  60. data/lib/active_admin/arbre/text_node.rb +35 -0
  61. data/lib/active_admin/callbacks.rb +87 -0
  62. data/lib/active_admin/comments.rb +73 -0
  63. data/lib/active_admin/comments/comment.rb +15 -0
  64. data/lib/active_admin/comments/configuration.rb +16 -0
  65. data/lib/active_admin/comments/namespace_helper.rb +14 -0
  66. data/lib/active_admin/comments/resource_helper.rb +17 -0
  67. data/lib/active_admin/comments/show_page_helper.rb +23 -0
  68. data/lib/active_admin/comments/views.rb +3 -0
  69. data/lib/active_admin/comments/views/active_admin_comment.rb +0 -0
  70. data/lib/active_admin/comments/views/active_admin_comments.rb +82 -0
  71. data/lib/active_admin/component.rb +22 -0
  72. data/lib/active_admin/controller_action.rb +12 -0
  73. data/lib/active_admin/dashboards.rb +0 -2
  74. data/lib/active_admin/dashboards/dashboard_controller.rb +10 -5
  75. data/lib/active_admin/dashboards/section.rb +5 -14
  76. data/lib/active_admin/devise.rb +43 -0
  77. data/lib/active_admin/dsl.rb +202 -0
  78. data/lib/active_admin/event.rb +31 -0
  79. data/lib/active_admin/form_builder.rb +68 -50
  80. data/lib/active_admin/iconic.rb +51 -0
  81. data/lib/active_admin/iconic/icons.rb +142 -0
  82. data/lib/active_admin/namespace.rb +102 -16
  83. data/lib/active_admin/renderer.rb +18 -3
  84. data/lib/active_admin/resource.rb +110 -9
  85. data/lib/active_admin/resource/belongs_to.rb +31 -0
  86. data/lib/active_admin/resource_controller.rb +45 -260
  87. data/lib/active_admin/resource_controller/action_builder.rb +21 -0
  88. data/lib/active_admin/resource_controller/actions.rb +53 -0
  89. data/lib/active_admin/resource_controller/callbacks.rb +47 -0
  90. data/lib/active_admin/resource_controller/collection.rb +142 -0
  91. data/lib/active_admin/resource_controller/filters.rb +58 -0
  92. data/lib/active_admin/resource_controller/form.rb +42 -0
  93. data/lib/active_admin/resource_controller/menu.rb +29 -0
  94. data/lib/active_admin/resource_controller/page_configurations.rb +53 -0
  95. data/lib/active_admin/resource_controller/scoping.rb +36 -0
  96. data/lib/active_admin/resource_controller/sidebars.rb +38 -0
  97. data/lib/active_admin/scope.rb +17 -0
  98. data/lib/active_admin/sidebar.rb +8 -47
  99. data/lib/active_admin/stylesheets/active_admin.scss +418 -0
  100. data/lib/active_admin/stylesheets/active_admin/_comments.scss +40 -0
  101. data/lib/active_admin/stylesheets/active_admin/_flash_messages.scss +13 -0
  102. data/lib/active_admin/stylesheets/active_admin/_forms.scss +168 -0
  103. data/lib/active_admin/stylesheets/active_admin/_header.scss +108 -0
  104. data/lib/active_admin/stylesheets/active_admin/_typography.scss +100 -0
  105. data/lib/active_admin/stylesheets/active_admin/mixins/_all.scss +8 -0
  106. data/lib/active_admin/stylesheets/active_admin/mixins/_buttons.scss +27 -0
  107. data/lib/active_admin/stylesheets/active_admin/mixins/_gradients.scss +21 -0
  108. data/lib/active_admin/stylesheets/active_admin/mixins/_icons.scss +20 -0
  109. data/lib/active_admin/stylesheets/active_admin/mixins/_reset.scss +165 -0
  110. data/lib/active_admin/stylesheets/active_admin/mixins/_rounded.scss +43 -0
  111. data/lib/active_admin/stylesheets/active_admin/mixins/_sections.scss +27 -0
  112. data/lib/active_admin/stylesheets/active_admin/mixins/_shadows.scss +21 -0
  113. data/lib/active_admin/stylesheets/active_admin/mixins/_variables.scss +12 -0
  114. data/lib/active_admin/version.rb +1 -1
  115. data/lib/active_admin/view_factory.rb +21 -0
  116. data/lib/active_admin/view_helpers.rb +17 -100
  117. data/lib/active_admin/view_helpers/assigns_with_indifferent_access_helper.rb +7 -0
  118. data/lib/active_admin/view_helpers/auto_link_helper.rb +41 -0
  119. data/lib/active_admin/view_helpers/breadcrumb_helper.rb +29 -0
  120. data/lib/active_admin/view_helpers/display_helper.rb +38 -0
  121. data/lib/active_admin/view_helpers/filter_form_helper.rb +174 -0
  122. data/lib/active_admin/view_helpers/form_helper.rb +12 -0
  123. data/lib/active_admin/view_helpers/icon_helper.rb +12 -0
  124. data/lib/active_admin/view_helpers/method_or_proc_helper.rb +26 -0
  125. data/lib/active_admin/view_helpers/renderer_helper.rb +29 -0
  126. data/lib/active_admin/view_helpers/sidebar_helper.rb +15 -0
  127. data/lib/active_admin/view_helpers/status_tag_helper.rb +13 -0
  128. data/lib/active_admin/view_helpers/table_helper.rb +12 -0
  129. data/lib/active_admin/view_helpers/title_helper.rb +11 -0
  130. data/lib/active_admin/view_helpers/view_factory_helper.rb +11 -0
  131. data/lib/active_admin/views.rb +8 -0
  132. data/lib/active_admin/views/action_items.rb +17 -0
  133. data/lib/active_admin/views/components/attributes_table.rb +66 -0
  134. data/lib/active_admin/views/components/columns.rb +49 -0
  135. data/lib/active_admin/views/components/paginated_collection.rb +74 -0
  136. data/lib/active_admin/views/components/panel.rb +26 -0
  137. data/lib/active_admin/views/components/scopes.rb +64 -0
  138. data/lib/active_admin/views/components/sidebar_section.rb +30 -0
  139. data/lib/active_admin/views/components/table_for.rb +174 -0
  140. data/lib/active_admin/views/dashboard_section_renderer.rb +19 -0
  141. data/lib/active_admin/views/header_renderer.rb +37 -0
  142. data/lib/active_admin/views/index_as_block.rb +24 -0
  143. data/lib/active_admin/views/index_as_blog.rb +89 -0
  144. data/lib/active_admin/views/index_as_grid.rb +49 -0
  145. data/lib/active_admin/views/index_as_table.rb +73 -0
  146. data/lib/active_admin/views/pages/base.rb +138 -0
  147. data/lib/active_admin/views/pages/dashboard.rb +61 -0
  148. data/lib/active_admin/views/pages/edit.rb +27 -0
  149. data/lib/active_admin/views/pages/index.rb +64 -0
  150. data/lib/active_admin/views/pages/new.rb +27 -0
  151. data/lib/active_admin/views/pages/show.rb +56 -0
  152. data/lib/active_admin/views/tabs_renderer.rb +52 -0
  153. data/lib/active_admin/views/templates/active_admin/devise/mailer/reset_password_instructions.html.erb +8 -0
  154. data/lib/active_admin/views/templates/active_admin/devise/mailer/unlock_instructions.html.erb +7 -0
  155. data/lib/active_admin/views/templates/active_admin/devise/passwords/edit.html.erb +16 -0
  156. data/lib/active_admin/views/templates/active_admin/devise/passwords/new.html.erb +14 -0
  157. data/lib/active_admin/views/templates/active_admin/devise/sessions/new.html.erb +18 -0
  158. data/lib/active_admin/views/templates/active_admin/devise/shared/_links.erb +20 -0
  159. data/lib/active_admin/views/templates/active_admin/devise/unlocks/new.html.erb +12 -0
  160. data/lib/active_admin/views/templates/active_admin_dashboard/index.html.arb +1 -0
  161. data/lib/active_admin/views/templates/active_admin_default/edit.html.arb +1 -0
  162. data/lib/active_admin/views/{active_admin_default → templates/active_admin_default}/index.csv.erb +0 -0
  163. data/lib/active_admin/views/templates/active_admin_default/index.html.arb +1 -0
  164. data/lib/active_admin/views/templates/active_admin_default/new.html.arb +1 -0
  165. data/lib/active_admin/views/templates/active_admin_default/show.html.arb +1 -0
  166. data/lib/active_admin/views/{layouts → templates/layouts}/active_admin.html.erb +3 -4
  167. data/lib/active_admin/views/templates/layouts/active_admin_logged_out.html.erb +31 -0
  168. data/lib/generators/active_admin/devise/devise_generator.rb +45 -0
  169. data/lib/generators/active_admin/install/install_generator.rb +18 -3
  170. data/lib/generators/active_admin/install/templates/active_admin.js +41 -0
  171. data/lib/generators/active_admin/install/templates/{active_admin.rb → active_admin.rb.erb} +33 -3
  172. data/lib/generators/active_admin/install/templates/active_admin_vendor.js +1 -1
  173. data/lib/generators/active_admin/install/templates/dashboards.rb +3 -4
  174. data/lib/generators/active_admin/install/templates/images/admin_notes_icon.png +0 -0
  175. data/lib/generators/active_admin/install/templates/images/loading.gif +0 -0
  176. data/lib/generators/active_admin/install/templates/images/nested_menu_arrow.gif +0 -0
  177. data/lib/generators/active_admin/install/templates/images/nested_menu_arrow_dark.gif +0 -0
  178. data/lib/generators/active_admin/install/templates/images/orderable.png +0 -0
  179. data/lib/generators/active_admin/install/templates/migrations/1_create_admin_notes.rb +16 -0
  180. data/lib/generators/active_admin/install/templates/migrations/2_move_admin_notes_to_comments.rb +25 -0
  181. data/spec/integration/belongs_to_spec.rb +42 -0
  182. data/spec/integration/index_as_table_spec.rb +0 -119
  183. data/spec/integration/layout_spec.rb +3 -37
  184. data/spec/spec_helper.rb +89 -50
  185. data/spec/support/integration_example_group.rb +37 -0
  186. data/spec/support/rails_template.rb +21 -9
  187. data/spec/support/rails_template_with_data.rb +32 -0
  188. data/spec/support/templates/cucumber.rb +28 -0
  189. data/spec/unit/abstract_view_factory_spec.rb +79 -0
  190. data/spec/unit/action_builder_spec.rb +19 -7
  191. data/spec/unit/action_items_spec.rb +16 -30
  192. data/spec/unit/active_admin_spec.rb +10 -22
  193. data/spec/unit/arbre/html/element_finder_methods_spec.rb +54 -0
  194. data/spec/unit/arbre/html/element_spec.rb +224 -0
  195. data/spec/unit/arbre/html/tag_attributes_spec.rb +61 -0
  196. data/spec/unit/arbre/html/tag_spec.rb +63 -0
  197. data/spec/unit/arbre/html_spec.rb +185 -0
  198. data/spec/unit/auto_link_spec.rb +38 -0
  199. data/spec/unit/belongs_to_spec.rb +44 -0
  200. data/spec/unit/breadcrumbs_spec.rb +110 -0
  201. data/spec/unit/comments_spec.rb +53 -0
  202. data/spec/unit/component_spec.rb +18 -0
  203. data/spec/unit/components/attributes_table_spec.rb +100 -0
  204. data/spec/unit/components/columns_spec.rb +75 -0
  205. data/spec/unit/components/panel_spec.rb +31 -0
  206. data/spec/unit/components/sidebar_section_spec.rb +37 -0
  207. data/spec/unit/components/table_for_spec.rb +130 -0
  208. data/spec/unit/dashboard_controller_spec.rb +26 -0
  209. data/spec/unit/dashboard_section_spec.rb +10 -17
  210. data/spec/unit/display_name_spec.rb +29 -0
  211. data/spec/unit/event_spec.rb +41 -0
  212. data/spec/unit/filter_form_builder_spec.rb +99 -77
  213. data/spec/unit/form_builder_spec.rb +69 -69
  214. data/spec/unit/namespace_spec.rb +61 -3
  215. data/spec/unit/pretty_format_spec.rb +35 -0
  216. data/spec/unit/registration_spec.rb +40 -1
  217. data/spec/unit/renderer_spec.rb +8 -0
  218. data/spec/unit/resource_controller_spec.rb +105 -6
  219. data/spec/unit/resource_spec.rb +78 -11
  220. data/spec/unit/routing_spec.rb +82 -6
  221. data/spec/unit/scope_spec.rb +31 -0
  222. data/spec/unit/tabs_renderer_spec.rb +24 -2
  223. data/spec/unit/view_factory_spec.rb +16 -0
  224. metadata +268 -82
  225. data/.gitignore +0 -25
  226. data/active_admin.gemspec +0 -22
  227. data/lib/active_admin/action_builder.rb +0 -60
  228. data/lib/active_admin/breadcrumbs.rb +0 -26
  229. data/lib/active_admin/dashboards/renderer.rb +0 -45
  230. data/lib/active_admin/dashboards/section_renderer.rb +0 -28
  231. data/lib/active_admin/filters.rb +0 -189
  232. data/lib/active_admin/pages.rb +0 -11
  233. data/lib/active_admin/pages/base.rb +0 -92
  234. data/lib/active_admin/pages/edit.rb +0 -21
  235. data/lib/active_admin/pages/index.rb +0 -58
  236. data/lib/active_admin/pages/index/blog.rb +0 -65
  237. data/lib/active_admin/pages/index/table.rb +0 -48
  238. data/lib/active_admin/pages/index/thumbnails.rb +0 -40
  239. data/lib/active_admin/pages/new.rb +0 -21
  240. data/lib/active_admin/pages/show.rb +0 -54
  241. data/lib/active_admin/table_builder.rb +0 -162
  242. data/lib/active_admin/tabs_renderer.rb +0 -39
  243. data/lib/active_admin/views/active_admin_dashboard/index.html.erb +0 -1
  244. data/lib/active_admin/views/active_admin_default/edit.html.erb +0 -1
  245. data/lib/active_admin/views/active_admin_default/index.html.erb +0 -1
  246. data/lib/active_admin/views/active_admin_default/new.html.erb +0 -1
  247. data/lib/active_admin/views/active_admin_default/show.html.erb +0 -1
  248. data/lib/generators/active_admin/install/templates/active_admin.css +0 -325
  249. data/lib/generators/active_admin/install/templates/images/orderable.gif +0 -0
  250. data/spec/integration/index_as_blog_spec.rb +0 -65
  251. data/spec/integration/index_as_thumbnails_spec.rb +0 -43
  252. data/spec/integration/new_view_spec.rb +0 -52
  253. data/spec/integration/show_view_spec.rb +0 -91
  254. data/spec/unit/sidebar_spec.rb +0 -96
  255. data/spec/unit/table_builder_spec.rb +0 -162
@@ -2,11 +2,11 @@ require 'active_admin/helpers/optional_display'
2
2
 
3
3
  module ActiveAdmin
4
4
  module ActionItems
5
+ extend ActiveSupport::Concern
5
6
 
6
- def self.included(base)
7
- base.send :extend, ClassMethods
8
- base.class_inheritable_accessor :action_items
9
- base.action_items = []
7
+ included do
8
+ self.class_inheritable_accessor :action_items
9
+ self.action_items = []
10
10
  end
11
11
 
12
12
  module ClassMethods
@@ -33,16 +33,5 @@ module ActiveAdmin
33
33
  normalize_display_options!
34
34
  end
35
35
  end
36
-
37
- class Renderer < ActiveAdmin::Renderer
38
- def to_html(action_items)
39
- content_tag :div, :class => 'action_items' do
40
- action_items.collect do |action_item|
41
- instance_eval(&action_item.block)
42
- end.join(" ")
43
- end
44
- end
45
- end
46
-
47
36
  end
48
37
  end
@@ -0,0 +1,23 @@
1
+ require "active_admin/arbre/html"
2
+ require "active_admin/arbre/attributes"
3
+ require "active_admin/arbre/core_extensions"
4
+ require "active_admin/arbre/element"
5
+ require "active_admin/arbre/context"
6
+ require "active_admin/arbre/collection"
7
+ require "active_admin/arbre/class_list"
8
+ require "active_admin/arbre/tag"
9
+ require "active_admin/arbre/document"
10
+ require "active_admin/arbre/html5_elements"
11
+ require "active_admin/arbre/text_node"
12
+
13
+ # Arbre - The DOM Tree in Ruby
14
+ #
15
+ # Arbre is a ruby library for building HTML in pure Object Oriented Ruby
16
+ module Arbre
17
+ end
18
+
19
+ require 'action_view'
20
+
21
+ ActionView::Template.register_template_handler :arb, lambda { |template|
22
+ "self.class.send :include, Arbre::HTML; @_helpers = self; begin; #{template.source}; end; current_dom_context"
23
+ }
@@ -0,0 +1,20 @@
1
+ module Arbre
2
+ module HTML
3
+
4
+ class Attributes < Hash
5
+
6
+ def to_html
7
+ self.collect do |name, value|
8
+ "#{html_escape(name)}=\"#{html_escape(value)}\""
9
+ end.join(" ")
10
+ end
11
+
12
+ protected
13
+
14
+ def html_escape(s)
15
+ ERB::Util.html_escape(s)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ require 'set'
2
+
3
+ module Arbre
4
+ module HTML
5
+
6
+ # Holds a set of classes
7
+ class ClassList < Set
8
+
9
+ def add(class_names)
10
+ class_names.to_s.split(" ").each do |class_name|
11
+ super(class_name)
12
+ end
13
+ self
14
+ end
15
+ alias :<< :add
16
+
17
+ def to_s
18
+ to_html
19
+ end
20
+
21
+ def to_html
22
+ to_a.join(" ")
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ module Arbre
2
+ module HTML
3
+
4
+ # Stores a collection of Element objects
5
+ class Collection < Array
6
+
7
+ def +(other)
8
+ self.class.new(super)
9
+ end
10
+
11
+ def -(other)
12
+ self.class.new(super)
13
+ end
14
+
15
+ def &(other)
16
+ self.class.new(super)
17
+ end
18
+
19
+ def to_html
20
+ self.collect do |element|
21
+ element.to_html
22
+ end.join.html_safe
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ module Arbre
2
+ class Context < Arbre::HTML::Element
3
+ def indent_level
4
+ # A context does not increment the indent_level
5
+ super - 1
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def to_html
3
+ to_s
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ module Arbre
2
+ module HTML
3
+
4
+ class Document < Tag
5
+
6
+ def build(*args)
7
+ super
8
+ build_head
9
+ build_body
10
+ end
11
+
12
+ def document
13
+ self
14
+ end
15
+
16
+ def tag_name
17
+ 'html'
18
+ end
19
+
20
+ def doctype
21
+ '<!DOCTYPE html>'.html_safe
22
+ end
23
+
24
+ def to_html
25
+ doctype + super
26
+ end
27
+
28
+ protected
29
+
30
+ def build_head
31
+ @head = head do
32
+ meta :"http-equiv" => "Content-type", :content => "text/html; charset=utf-8"
33
+ end
34
+ end
35
+
36
+ def build_body
37
+ @body = body
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,151 @@
1
+ module Arbre
2
+ module HTML
3
+
4
+ class Element
5
+ include ::Arbre::HTML
6
+ include ::Arbre::HTML::BuilderMethods
7
+
8
+ attr_accessor :parent
9
+ attr_reader :children
10
+
11
+ def self.builder_method(method_name)
12
+ ::Arbre::HTML::BuilderMethods.class_eval <<-EOF, __FILE__, __LINE__
13
+ def #{method_name}(*args, &block)
14
+ insert_tag #{self.name}, *args, &block
15
+ end
16
+ EOF
17
+ end
18
+
19
+ def initialize(assigns = {}, helpers = nil)
20
+ @_assigns, @_helpers = assigns, helpers
21
+ @children = Collection.new
22
+ end
23
+
24
+ def assigns
25
+ @_assigns
26
+ end
27
+
28
+ def helpers
29
+ @_helpers
30
+ end
31
+
32
+ def tag_name
33
+ @tag_name ||= self.class.name.demodulize.downcase
34
+ end
35
+
36
+ def build(*args, &block)
37
+ # Render the block passing ourselves in
38
+ insert_text_node_if_string(block.call(self)) if block
39
+ end
40
+
41
+ def add_child(child)
42
+ return unless child
43
+
44
+ if child.is_a?(Array)
45
+ child.each{|item| add_child(item) }
46
+ return @children
47
+ end
48
+
49
+ # If its not an element, wrap it in a TextNode
50
+ unless child.is_a?(Element)
51
+ child = TextNode.from_string(child)
52
+ end
53
+
54
+ if child.respond_to?(:parent)
55
+ # Remove the child
56
+ child.parent.remove_child(child) if child.parent
57
+ # Set ourselves as the parent
58
+ child.parent = self
59
+ end
60
+
61
+ @children << child
62
+ end
63
+
64
+ def remove_child(child)
65
+ child.parent = nil if child.respond_to?(:parent=)
66
+ @children.delete(child)
67
+ end
68
+
69
+ def <<(child)
70
+ add_child(child)
71
+ end
72
+
73
+ def parent=(parent)
74
+ @parent = parent
75
+ end
76
+
77
+ def parent?
78
+ !@parent.nil?
79
+ end
80
+
81
+ def document
82
+ parent.document if parent?
83
+ end
84
+
85
+ def content=(contents)
86
+ clear_children!
87
+ add_child(contents)
88
+ end
89
+
90
+ def get_elements_by_tag_name(tag_name)
91
+ elements = Collection.new
92
+ children.each do |child|
93
+ elements << child if child.tag_name == tag_name
94
+ elements.concat(child.get_elements_by_tag_name(tag_name))
95
+ end
96
+ elements
97
+ end
98
+ alias_method :find_by_tag, :get_elements_by_tag_name
99
+
100
+ def content
101
+ children.to_html
102
+ end
103
+
104
+ def html_safe
105
+ to_html
106
+ end
107
+
108
+ def indent_level
109
+ parent? ? parent.indent_level + 1 : 0
110
+ end
111
+
112
+ def each(&block)
113
+ [to_html].each(&block)
114
+ end
115
+
116
+ def to_s
117
+ to_html
118
+ end
119
+
120
+ def to_str
121
+ to_s
122
+ end
123
+
124
+ def to_html
125
+ content
126
+ end
127
+
128
+ def +(element)
129
+ case element
130
+ when Element, Collection
131
+ else
132
+ element = TextNode.from_string(element)
133
+ end
134
+ Collection.new([self]) + element
135
+ end
136
+
137
+ def to_ary
138
+ Collection.new [self]
139
+ end
140
+ alias_method :to_a, :to_ary
141
+
142
+ private
143
+
144
+ # Resets the Elements children
145
+ def clear_children!
146
+ @children.clear
147
+ end
148
+ end
149
+
150
+ end
151
+ end
@@ -0,0 +1,84 @@
1
+ module Arbre
2
+ module HTML
3
+ def current_dom_context
4
+ @__current_dom_element__ ||= Arbre::Context.new(assigns, helpers)
5
+ @__current_dom_element__.current_dom_context
6
+ end
7
+
8
+ def helpers
9
+ @_helpers
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ if current_dom_context.respond_to?(name)
14
+ current_dom_context.send name, *args, &block
15
+ elsif assigns && assigns.has_key?(name)
16
+ assigns[name]
17
+ elsif helpers.respond_to?(name)
18
+ helpers.send(name, *args, &block)
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ module BuilderMethods
25
+ def build_tag(klass, *args, &block)
26
+ tag = klass.new(assigns, helpers)
27
+
28
+ # If you passed in a block and want the object
29
+ if block_given? && block.arity > 0
30
+ # Set out context to the tag, and pass responsibility to the tag
31
+ with_current_dom_context tag do
32
+ tag.build(*args, &block)
33
+ end
34
+ else
35
+ # Build the tag
36
+ tag.build(*args)
37
+
38
+ # Render the blocks contents
39
+ if block_given?
40
+ with_current_dom_context tag do
41
+ insert_text_node_if_string(yield)
42
+ end
43
+ end
44
+ end
45
+
46
+ tag
47
+ end
48
+
49
+ def insert_tag(klass, *args, &block)
50
+ tag = build_tag(klass, *args, &block)
51
+ current_dom_context.add_child(tag)
52
+ tag
53
+ end
54
+
55
+ def current_dom_context
56
+ @__current_dom_element_buffer__ ||= [self]
57
+ current_element = @__current_dom_element_buffer__.last
58
+ if current_element == self
59
+ self
60
+ else
61
+ current_element.current_dom_context
62
+ end
63
+ end
64
+
65
+ def with_current_dom_context(tag)
66
+ raise ArgumentError, "Can't be in the context of nil. #{@__current_dom_element_buffer__.inspect}" unless tag
67
+ current_dom_context # Ensure a context is setup
68
+ @__current_dom_element_buffer__.push tag
69
+ yield
70
+ @__current_dom_element_buffer__.pop
71
+ end
72
+ alias_method :within, :with_current_dom_context
73
+
74
+ # Inserts a text node if the tag is a string
75
+ def insert_text_node_if_string(tag)
76
+ if tag.is_a?(String)
77
+ current_dom_context << TextNode.from_string(tag)
78
+ end
79
+ end
80
+ end
81
+
82
+ end
83
+ end
84
+
@@ -0,0 +1,47 @@
1
+ module Arbre
2
+ module HTML
3
+
4
+ AUTO_BUILD_ELEMENTS = [ :a, :abbr, :address, :area, :article, :aside, :audio, :b, :base,
5
+ :bdo, :blockquote, :body, :br, :button, :canvas, :caption, :cite,
6
+ :code, :col, :colgroup, :command, :datalist, :dd, :del, :details,
7
+ :dfn, :div, :dl, :dt, :em, :embed, :fieldset, :figcaption, :figure,
8
+ :footer, :form, :h1, :h2, :h3, :h4, :h5, :h6, :head, :header, :hgroup,
9
+ :hr, :html, :i, :iframe, :img, :input, :ins, :keygen, :kbd, :label,
10
+ :legend, :li, :link, :map, :mark, :menu, :meta, :meter, :nav, :noscript,
11
+ :object, :ol, :optgroup, :option, :output, :pre, :progress, :q,
12
+ :s, :samp, :script, :section, :select, :small, :source, :span,
13
+ :strong, :style, :sub, :summary, :sup, :table, :tbody, :td,
14
+ :textarea, :tfoot, :th, :thead, :time, :title, :tr, :ul, :var, :video ]
15
+
16
+ HTML5_ELEMENTS = [ :p ] + AUTO_BUILD_ELEMENTS
17
+
18
+ AUTO_BUILD_ELEMENTS.each do |name|
19
+ class_eval <<-EOF
20
+ class #{name.to_s.capitalize} < Tag
21
+ builder_method :#{name}
22
+ end
23
+ EOF
24
+ end
25
+
26
+ class P < Tag
27
+ builder_method :para
28
+ end
29
+
30
+ class Table < Tag
31
+ def initialize(*)
32
+ super
33
+ set_table_tag_defaults
34
+ end
35
+
36
+ protected
37
+
38
+ # Set some good defaults for tables
39
+ def set_table_tag_defaults
40
+ set_attribute :border, 0
41
+ set_attribute :cellspacing, 0
42
+ set_attribute :cellpadding, 0
43
+ end
44
+ end
45
+
46
+ end
47
+ end