radiant-templates-extension 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/HELP_designer.textile +79 -0
  2. data/README.textile +123 -0
  3. data/Rakefile +136 -0
  4. data/VERSION +1 -0
  5. data/app/controllers/admin/part_types_controller.rb +8 -0
  6. data/app/controllers/admin/templates_controller.rb +38 -0
  7. data/app/helpers/admin/part_types_helper.rb +26 -0
  8. data/app/helpers/admin/templates_helper.rb +108 -0
  9. data/app/models/part_type.rb +14 -0
  10. data/app/models/template.rb +80 -0
  11. data/app/models/template_part.rb +21 -0
  12. data/app/views/admin/pages/_edit_template.html.haml +32 -0
  13. data/app/views/admin/pages/_edit_template_part.html.haml +20 -0
  14. data/app/views/admin/pages/_switch_templates.html.haml +23 -0
  15. data/app/views/admin/pages/_template_column.html.haml +3 -0
  16. data/app/views/admin/pages/_template_column_header.html.haml +1 -0
  17. data/app/views/admin/part_types/_form.html.haml +36 -0
  18. data/app/views/admin/part_types/_list.html.haml +23 -0
  19. data/app/views/admin/part_types/edit.html.haml +5 -0
  20. data/app/views/admin/part_types/index.html.haml +7 -0
  21. data/app/views/admin/part_types/new.html.haml +2 -0
  22. data/app/views/admin/part_types/remove.html.haml +15 -0
  23. data/app/views/admin/templates/_form.html.haml +65 -0
  24. data/app/views/admin/templates/_template_part.html.haml +21 -0
  25. data/app/views/admin/templates/_templates_css.html.haml +12 -0
  26. data/app/views/admin/templates/edit.html.haml +10 -0
  27. data/app/views/admin/templates/index.html.haml +47 -0
  28. data/app/views/admin/templates/new.html.haml +9 -0
  29. data/app/views/admin/templates/remove.html.haml +15 -0
  30. data/config/initializers/radiant_config.rb +3 -0
  31. data/config/locales/en.yml +32 -0
  32. data/config/routes.rb +7 -0
  33. data/db/migrate/001_create_templates.rb +17 -0
  34. data/db/migrate/002_create_template_parts.rb +15 -0
  35. data/db/migrate/003_rename_sublayout_column.rb +9 -0
  36. data/db/migrate/004_add_position_to_templates.rb +13 -0
  37. data/db/migrate/005_create_part_types.rb +17 -0
  38. data/db/migrate/006_add_description_to_template_parts.rb +9 -0
  39. data/db/migrate/007_add_page_class_name_to_templates.rb +9 -0
  40. data/db/migrate/008_add_index_to_templates.rb +9 -0
  41. data/db/migrate/009_create_default_part_types.rb +23 -0
  42. data/db/migrate/010_update_boolean_part_type.rb +9 -0
  43. data/db/migrate/011_add_timestamps.rb +19 -0
  44. data/lib/file_system/model/part_type_extensions.rb +19 -0
  45. data/lib/file_system/model/template_extensions.rb +52 -0
  46. data/lib/tasks/templates_extension_tasks.rake +28 -0
  47. data/lib/templates/associations.rb +7 -0
  48. data/lib/templates/controller_extensions.rb +20 -0
  49. data/lib/templates/helper.rb +75 -0
  50. data/lib/templates/page_extensions.rb +43 -0
  51. data/lib/templates/tags.rb +43 -0
  52. data/public/images/admin/menu_arrow.png +0 -0
  53. data/public/images/admin/move_higher.png +0 -0
  54. data/public/images/admin/move_lower.png +0 -0
  55. data/public/images/admin/move_to_bottom.png +0 -0
  56. data/public/images/admin/move_to_top.png +0 -0
  57. data/public/images/admin/part_type.png +0 -0
  58. data/public/images/admin/template.png +0 -0
  59. data/spec/controllers/admin_page_controller_extensions_spec.rb +61 -0
  60. data/spec/controllers/part_types_controller_spec.rb +89 -0
  61. data/spec/controllers/templates_controller_spec.rb +322 -0
  62. data/spec/datasets/part_types_dataset.rb +15 -0
  63. data/spec/datasets/templates_dataset.rb +34 -0
  64. data/spec/helpers/templates_helper_spec.rb +56 -0
  65. data/spec/models/page_extensions_spec.rb +9 -0
  66. data/spec/models/part_type_spec.rb +28 -0
  67. data/spec/models/tags_spec.rb +44 -0
  68. data/spec/models/template_part_spec.rb +37 -0
  69. data/spec/models/template_spec.rb +88 -0
  70. data/spec/spec.opts +6 -0
  71. data/spec/spec_helper.rb +39 -0
  72. data/templates_extension.rb +83 -0
  73. data/vendor/plugins/acts_as_list/README +23 -0
  74. data/vendor/plugins/acts_as_list/init.rb +3 -0
  75. data/vendor/plugins/acts_as_list/lib/active_record/acts/list.rb +256 -0
  76. data/vendor/plugins/acts_as_list/test/list_test.rb +332 -0
  77. data/vendor/plugins/make_resourceful/DEFAULTS +148 -0
  78. data/vendor/plugins/make_resourceful/LICENSE +7 -0
  79. data/vendor/plugins/make_resourceful/README +239 -0
  80. data/vendor/plugins/make_resourceful/Rakefile +42 -0
  81. data/vendor/plugins/make_resourceful/VERSION +1 -0
  82. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/resourceful_scaffold_generator.rb +87 -0
  83. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/controller.rb +5 -0
  84. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/fixtures.yml +10 -0
  85. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/functional_test.rb +57 -0
  86. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/helper.rb +2 -0
  87. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/migration.rb +13 -0
  88. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/model.rb +2 -0
  89. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/unit_test.rb +7 -0
  90. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view__form.haml +5 -0
  91. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_edit.haml +11 -0
  92. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_index.haml +5 -0
  93. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_new.haml +9 -0
  94. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_partial.haml +12 -0
  95. data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_show.haml +14 -0
  96. data/vendor/plugins/make_resourceful/init.rb +3 -0
  97. data/vendor/plugins/make_resourceful/lib/resourceful/base.rb +52 -0
  98. data/vendor/plugins/make_resourceful/lib/resourceful/builder.rb +354 -0
  99. data/vendor/plugins/make_resourceful/lib/resourceful/default/accessors.rb +346 -0
  100. data/vendor/plugins/make_resourceful/lib/resourceful/default/actions.rb +92 -0
  101. data/vendor/plugins/make_resourceful/lib/resourceful/default/callbacks.rb +51 -0
  102. data/vendor/plugins/make_resourceful/lib/resourceful/default/responses.rb +118 -0
  103. data/vendor/plugins/make_resourceful/lib/resourceful/default/urls.rb +137 -0
  104. data/vendor/plugins/make_resourceful/lib/resourceful/maker.rb +83 -0
  105. data/vendor/plugins/make_resourceful/lib/resourceful/response.rb +33 -0
  106. data/vendor/plugins/make_resourceful/lib/resourceful/serialize.rb +181 -0
  107. data/vendor/plugins/make_resourceful/spec/accessors_spec.rb +473 -0
  108. data/vendor/plugins/make_resourceful/spec/actions_spec.rb +283 -0
  109. data/vendor/plugins/make_resourceful/spec/base_spec.rb +12 -0
  110. data/vendor/plugins/make_resourceful/spec/builder_spec.rb +332 -0
  111. data/vendor/plugins/make_resourceful/spec/callbacks_spec.rb +71 -0
  112. data/vendor/plugins/make_resourceful/spec/integration_spec.rb +396 -0
  113. data/vendor/plugins/make_resourceful/spec/maker_spec.rb +91 -0
  114. data/vendor/plugins/make_resourceful/spec/response_spec.rb +37 -0
  115. data/vendor/plugins/make_resourceful/spec/responses_spec.rb +314 -0
  116. data/vendor/plugins/make_resourceful/spec/rspec_on_rails/LICENSE +35 -0
  117. data/vendor/plugins/make_resourceful/spec/rspec_on_rails/redirect_to.rb +81 -0
  118. data/vendor/plugins/make_resourceful/spec/rspec_on_rails/render_template.rb +28 -0
  119. data/vendor/plugins/make_resourceful/spec/serialize_spec.rb +133 -0
  120. data/vendor/plugins/make_resourceful/spec/spec_helper.rb +279 -0
  121. data/vendor/plugins/make_resourceful/spec/urls_spec.rb +276 -0
  122. metadata +214 -0
@@ -0,0 +1,118 @@
1
+ module Resourceful
2
+ module Default
3
+ module Responses
4
+ # Sets the default flash message.
5
+ # This message can be overridden by passing in
6
+ # an HTTP parameter of the form "_flash[type]" via POST or GET.
7
+ #
8
+ # You can use this to easily have multiple forms
9
+ # post to the same create/edit/destroy actions
10
+ # but display different flash notices -
11
+ # without modifying the controller code at all.
12
+ #
13
+ # By default, the flash types are +notice+ when the database operation completes successfully
14
+ # and +error+ when it fails.
15
+ #
16
+ #--
17
+ # TODO: Move this out of here
18
+ #++
19
+ def set_default_flash(type, message)
20
+ flash[type] ||= (params[:_flash] && params[:_flash][type]) || message
21
+ end
22
+
23
+ # Sets the default redirect
24
+ # (the argument passed to +redirect_to+).
25
+ # This message can be overridden by passing in
26
+ # an HTTP parameter of the form "_redirect_on[status]" via POST or GET.
27
+ #
28
+ # You can use this to easily have multiple forms
29
+ # post to the same create/edit/destroy actions
30
+ # but redirect to different URLs -
31
+ # without modifying the controller code at all.
32
+ #
33
+ # By default, the redirect statuses are +success+ when the database operation completes successfully
34
+ # and +failure+ when it fails.
35
+ # Use the <tt>:status</tt> option to specify which status to run the redirect for.
36
+ # For example:
37
+ #
38
+ # set_default_redirect "/posts", :status => :failure
39
+ #
40
+ # This will run <tt>redirect_to params[:_redirect_on][:failure]</tt> if the parameter exists,
41
+ # or <tt>redirect_to "/posts"</tt> otherwise.
42
+ #
43
+ #--
44
+ # TODO: Move this out of here
45
+ #++
46
+ def set_default_redirect(to, options = {})
47
+ status = options[:status] || :success
48
+ redirect_to (params[:_redirect_on] && params[:_redirect_on][status]) || to
49
+ end
50
+
51
+ # This method is automatically run when this module is included in Resourceful::Base.
52
+ # It sets up the default responses for the default actions.
53
+ def self.included(base)
54
+ base.made_resourceful do
55
+ response_for(:show, :index, :edit, :new) do |format|
56
+ format.html
57
+ format.js
58
+ end
59
+
60
+ response_for(:show_fails) do |format|
61
+ not_found = Proc.new { render :text => "No item found", :status => 404 }
62
+ format.html &not_found
63
+ format.js &not_found
64
+ format.xml &not_found
65
+ end
66
+
67
+ response_for(:create) do |format|
68
+ format.html do
69
+ set_default_flash(:notice, "Create successful!")
70
+ set_default_redirect object_path
71
+ end
72
+ format.js
73
+ end
74
+
75
+ response_for(:create_fails) do |format|
76
+ format.html do
77
+ set_default_flash :error, "There was a problem!"
78
+ render :action => :new, :status => 422
79
+ end
80
+ format.js
81
+ end
82
+
83
+ response_for(:update) do |format|
84
+ format.html do
85
+ set_default_flash :notice, "Save successful!"
86
+ set_default_redirect object_path
87
+ end
88
+ format.js
89
+ end
90
+
91
+ response_for(:update_fails) do |format|
92
+ format.html do
93
+ set_default_flash :error, "There was a problem saving!"
94
+ render :action => :edit, :status => 422
95
+ end
96
+ format.js
97
+ end
98
+
99
+ response_for(:destroy) do |format|
100
+ format.html do
101
+ set_default_flash :notice, "Record deleted!"
102
+ set_default_redirect objects_path
103
+ end
104
+ format.js
105
+ end
106
+
107
+ response_for(:destroy_fails) do |format|
108
+ format.html do
109
+ set_default_flash :error, "There was a problem deleting!"
110
+ set_default_redirect :back, :status => :failure
111
+ end
112
+ format.js
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,137 @@
1
+ module Resourceful
2
+ module Default
3
+ # This file contains various methods to make URL helpers less painful.
4
+ # They provide methods analogous to the standard foo_url and foo_path helpers.
5
+ # However, they use make_resourceful's knowledge of the structure of the controller
6
+ # to allow you to avoid figuring out which method to call and which parent objects it should be passed.
7
+ module URLs
8
+ # This returns the path for the given object,
9
+ # by default current_object[link:classes/Resourceful/Default/Accessors.html#M000012].
10
+ # For example, in HatsController the following are equivalent:
11
+ #
12
+ # object_path #=> "/hats/12"
13
+ # hat_path(@hat) #=> "/hats/12"
14
+ #
15
+ def object_path(object = current_object); object_route(object, 'path'); end
16
+ # Same as object_path, but with the protocol and hostname.
17
+ def object_url (object = current_object); object_route(object, 'url'); end
18
+
19
+ # This is the same as object_path,
20
+ # unless a parent exists.
21
+ # Then it returns the nested path for the object.
22
+ # For example, in HatsController where Person has_many :hats and <tt>params[:person_id] == 42</tt>,
23
+ # the following are equivalent:
24
+ #
25
+ # nested_object_path #=> "/person/42/hats/12"
26
+ # person_hat_path(@person, @hat) #=> "/person/42/hats/12"
27
+ #
28
+ def nested_object_path(object = current_object); nested_object_route(object, 'path'); end
29
+ # Same as nested_object_path, but with the protocol and hostname.
30
+ def nested_object_url (object = current_object); nested_object_route(object, 'url'); end
31
+
32
+ # This returns the path for the edit action for the given object,
33
+ # by default current_object[link:classes/Resourceful/Default/Accessors.html#M000012].
34
+ # For example, in HatsController the following are equivalent:
35
+ #
36
+ # edit_object_path #=> "/hats/12/edit"
37
+ # edit_person_hat_path(@person, @hat) #=> "/hats/12/edit"
38
+ #
39
+ def edit_object_path(object = current_object); edit_object_route(object, 'path'); end
40
+ # Same as edit_object_path, but with the protocol and hostname.
41
+ def edit_object_url (object = current_object); edit_object_route(object, 'url'); end
42
+
43
+ # This returns the path for the collection of the current controller.
44
+ # For example, in HatsController where Person has_many :hats and <tt>params[:person_id] == 42</tt>,
45
+ # the following are equivalent:
46
+ #
47
+ # objects_path #=> "/people/42/hats"
48
+ # person_hats_path(@person) #=> "/people/42/hats"
49
+ #
50
+ def objects_path; objects_route('path'); end
51
+ # Same as objects_path, but with the protocol and hostname.
52
+ def objects_url ; objects_route('url'); end
53
+
54
+ # This returns the path for the new action for the current controller.
55
+ # For example, in HatsController where Person has_many :hats and <tt>params[:person_id] == 42</tt>,
56
+ # the following are equivalent:
57
+ #
58
+ # new_object_path #=> "/people/42/hats/new"
59
+ # new_person_hat_path(@person) #=> "/people/42/hats/new"
60
+ #
61
+ def new_object_path; new_object_route('path'); end
62
+ # Same as new_object_path, but with the protocol and hostname.
63
+ def new_object_url ; new_object_route('url'); end
64
+
65
+ # This returns the path for the parent object.
66
+ #
67
+ def parent_path(object = parent_object)
68
+ instance_route(parent_name, object, 'path')
69
+ end
70
+ # Same as parent_path, but with the protocol and hostname.
71
+ def parent_url(object = parent_object)
72
+ instance_route(parent_name, object, 'url')
73
+ end
74
+
75
+ # This prefix is added to the Rails URL helper names
76
+ # before they're called.
77
+ # By default, it's the underscored list of namespaces of the current controller,
78
+ # or nil if there are no namespaces defined.
79
+ # However, it can be overridden if another prefix is needed.
80
+ # Note that if this is overridden,
81
+ # the new method should return a string ending in an underscore.
82
+ #
83
+ # For example, in Admin::Content::PagesController:
84
+ #
85
+ # url_helper_prefix #=> "admin_content_"
86
+ #
87
+ # Then object_path is the same as <tt>admin_content_page_path(current_object)</tt>.
88
+ def url_helper_prefix
89
+ namespaces.empty? ? nil : "#{namespaces.join('_')}_"
90
+ end
91
+
92
+ # This prefix is added to the Rails URL helper names
93
+ # for the make_resourceful collection URL helpers,
94
+ # objects_path and new_object_path.
95
+ # It's only added if url_helper_prefix returns nil.
96
+ # By default, it's the parent name followed by an underscore if a parent is given,
97
+ # and the empty string otherwise.
98
+ #
99
+ # See also url_helper_prefix.
100
+ def collection_url_prefix
101
+ parent? ? "#{parent_name}_" : ''
102
+ end
103
+
104
+ private
105
+
106
+ def object_route(object, type)
107
+ instance_route(current_model_name.underscore, object, type)
108
+ end
109
+
110
+ def nested_object_route(object, type)
111
+ return object_route(object, type) unless parent?
112
+ send("#{url_helper_prefix}#{parent_name}_#{current_model_name.underscore}_#{type}", parent_object, object)
113
+ end
114
+
115
+ def edit_object_route(object, type)
116
+ instance_route(current_model_name.underscore, object, type, "edit")
117
+ end
118
+
119
+ def objects_route(type)
120
+ collection_route(current_model_name.pluralize.underscore, type)
121
+ end
122
+
123
+ def new_object_route(type)
124
+ collection_route(current_model_name.underscore, type, "new")
125
+ end
126
+
127
+ def instance_route(name, object, type, action = nil)
128
+ send("#{action ? action + '_' : ''}#{url_helper_prefix}#{name}_#{type}", object)
129
+ end
130
+
131
+ def collection_route(name, type, action = nil)
132
+ send("#{action ? action + '_' : ''}#{url_helper_prefix || collection_url_prefix}#{name}_#{type}",
133
+ *(parent? ? [parent_object] : []))
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,83 @@
1
+ require 'resourceful/builder'
2
+ require 'resourceful/base'
3
+
4
+ module Resourceful
5
+ # This module is extended by the ActionController::Base class object.
6
+ # It provides the actual +make_resourceful+ method
7
+ # and sets up the controller so that everything will work.
8
+ module Maker
9
+ # Called automatically on ActionController::Base.
10
+ # Initializes various inheritable attributes.
11
+ def self.extended(base)
12
+ base.write_inheritable_attribute :resourceful_callbacks, {}
13
+ base.write_inheritable_attribute :resourceful_responses, {}
14
+ base.write_inheritable_attribute :parents, []
15
+ base.write_inheritable_attribute :made_resourceful, false
16
+ end
17
+
18
+ # :call-seq:
19
+ # make_resourceful(options = {}) { ... }
20
+ #
21
+ # This is the central method, and namesake, of make_resourceful.
22
+ # It takes a block and evaluates it in the context of a Builder,
23
+ # allowing the controller to be customized extensively.
24
+ #
25
+ # See Resourceful::Builder for documentation on the methods available
26
+ # in the context of the block.
27
+ #
28
+ # The only option currently available is <tt>:include</tt>.
29
+ # It takes an object that responds to to_proc
30
+ # (or an array of such objects)
31
+ # and evaluates that proc in the same context as the block.
32
+ # For example:
33
+ #
34
+ # make_resourceful :include => proc { actions :all } do
35
+ # before :show do
36
+ # current_object.current_user = current_user
37
+ # end
38
+ # end
39
+ #
40
+ # This is the same as:
41
+ #
42
+ # make_resourceful do
43
+ # actions :all
44
+ # before :show do
45
+ # current_object.current_user = current_user
46
+ # end
47
+ # end
48
+ #
49
+ def make_resourceful(options = {}, &block)
50
+ # :stopdoc:
51
+ include Resourceful::Base
52
+ # :startdoc:
53
+
54
+ builder = Resourceful::Builder.new(self)
55
+ unless builder.inherited?
56
+ Resourceful::Base.made_resourceful.each { |proc| builder.instance_eval(&proc) }
57
+ end
58
+ Array(options[:include]).each { |proc| builder.instance_eval(&proc) }
59
+ builder.instance_eval(&block)
60
+
61
+ builder.apply
62
+
63
+ add_helpers
64
+ end
65
+
66
+ # Returns whether or not make_resourceful has been called
67
+ # on this controller or any controllers it inherits from.
68
+ def made_resourceful?
69
+ read_inheritable_attribute(:made_resourceful)
70
+ end
71
+
72
+ private
73
+
74
+ def add_helpers
75
+ helper_method(:object_path, :objects_path, :new_object_path, :edit_object_path,
76
+ :object_url, :objects_url, :new_object_url, :edit_object_url,
77
+ :parent_path, :parent_url,
78
+ :current_objects, :current_object, :current_model, :current_model_name,
79
+ :namespaces, :instance_variable_name, :parent_names, :parent_name,
80
+ :parent?, :parent_model, :parent_object, :save_succeeded?)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,33 @@
1
+ module Resourceful
2
+ # This is the class of the object passed to the Builder#response_for method.
3
+ # It shouldn't be used by users.
4
+ #
5
+ # The Response collects format procs
6
+ # and returns them with the format method,
7
+ # in the order they were given.
8
+ # For example:
9
+ #
10
+ # response.html { redirect_to '/' }
11
+ # response.xml { render :xml => current_object.to_xml }
12
+ # response.js
13
+ # response.formats #=> [[:html, #<Proc>], [:xml, #<Proc>], [:js, #<Proc>]]
14
+ #
15
+ # Note that the <tt>:js</tt> response is the empty proc -
16
+ # the same as <tt>proc {}</tt>.
17
+ class Response # :nodoc:
18
+ # Returns a list of pairs of formats and procs
19
+ # representing the formats passed to the response object.
20
+ # See class description.
21
+ attr :formats
22
+
23
+ # Returns a new Response with no format data.
24
+ def initialize
25
+ @formats = []
26
+ end
27
+
28
+ # Used to dispatch the individual format methods.
29
+ def method_missing(name, &block)
30
+ @formats.push([name, block || proc {}]) unless @formats.any? {|n,b| n == name}
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,181 @@
1
+ require 'resourceful/builder'
2
+
3
+ module Resourceful
4
+ # This module contains mixin modules
5
+ # used to implement the object serialization
6
+ # used for the Builder#publish method.
7
+ # They can also be used to get serialized representations of objects
8
+ # in other contexts.
9
+ #
10
+ # Serialization makes use of duck typing.
11
+ # Each class that can be serialized
12
+ # (just Array and ActiveRecord::Base by default)
13
+ # implements the +serialize+ and +to_serializable+ methods.
14
+ # These methods are implemented differently by the different classes,
15
+ # but the semantics of the implementations are consistent,
16
+ # so they can be used consistently.
17
+ #
18
+ # +to_serializable+ returns an object that can be directly serialized
19
+ # with a call to +to_xml+, +to_yaml+, or +to_json+.
20
+ # This object is either a hash or an array,
21
+ # and all the elements are either values, like strings and integers,
22
+ # or other serializable objects.
23
+ # This is useful for getting a model into a simple data structure format.
24
+ # The +attributes+ argument uses the same semantics
25
+ # as the <tt>:attributes</tt> option for Builder#publish.
26
+ # For example:
27
+ #
28
+ # c = Cake.new(:flavor => 'chocolate', :text => 'Happy birthday, Chris!')
29
+ # c.recipient = User.new(:name => 'Chris', :password => 'not very secure')
30
+ # c.to_serializable [
31
+ # :flavor, :text,
32
+ # :recipient => :name
33
+ # ]
34
+ #
35
+ # This would return the Ruby hash
36
+ #
37
+ # { :flavor => 'chocolate', :text => 'Happy birthday, Chris!',
38
+ # :user => {:name => 'Chris'} }
39
+ #
40
+ # +serialize+ takes a format (<tt>:xml</tt>, <tt>:yaml</tt>, or <tt>:json</tt> - see New Formats below)
41
+ # and a hash of options.
42
+ # The only option currently recognized is <tt>:attributes</tt>,
43
+ # which has the same semantics
44
+ # as the <tt>:attributes</tt> option for Builder#publish.
45
+ # +serialize+ returns a string containing the target
46
+ # serialized in the given format.
47
+ # For example:
48
+ #
49
+ # c = CandyBag.new(:title => 'jellybag')
50
+ # c.candies << Candy.new(:type => 'jellybean', :flavor => 'root beer')
51
+ # c.candies << Candy.new(:type => 'jellybean', :flavor => 'pear')
52
+ # c.candies << Candy.new(:type => 'licorice', :flavor => 'anisey')
53
+ # c.serialize :xml, :attributes => [:title, {:candies => [:type, :flavor]}]
54
+ #
55
+ # This would return a Ruby string containing
56
+ #
57
+ # <?xml version="1.0" encoding="UTF-8"?>
58
+ # <candy-bag>
59
+ # <title>jellybag</title>
60
+ # <candies>
61
+ # <candy>
62
+ # <type>jellybean</type>
63
+ # <flavor>root beer</flavor>
64
+ # </candy>
65
+ # <candy>
66
+ # <type>jellybean</type>
67
+ # <flavor>pear</flavor>
68
+ # </candy>
69
+ # <candy>
70
+ # <type>licorice</type>
71
+ # <flavor>anisey</flavor>
72
+ # </candy>
73
+ # </candies>
74
+ # </candy-bag>
75
+ #
76
+ module Serialize
77
+
78
+ # Takes an attributes option in the form passed to Builder#publish
79
+ # and returns a hash (or nil, if attributes is nil)
80
+ # containing the same data,
81
+ # but in a more consistent format.
82
+ # All keys are converted to symbols,
83
+ # and all lists are converted to hashes.
84
+ # For example:
85
+ #
86
+ # Resourceful::Serialize.normalize_attributes([:foo, :bar, {"baz" => ["boom"]}])
87
+ # #=> {"baz"=>["boom"], :foo=>nil, :bar=>nil}
88
+ #
89
+ def self.normalize_attributes(attributes) # :nodoc:
90
+ return nil if attributes.nil?
91
+ return {attributes.to_sym => nil} if String === attributes
92
+ return {attributes => nil} if !attributes.respond_to?(:inject)
93
+
94
+ attributes.inject({}) do |hash, attr|
95
+ if Array === attr
96
+ hash[attr[0]] = attr[1]
97
+ hash
98
+ else
99
+ hash.merge normalize_attributes(attr)
100
+ end
101
+ end
102
+ end
103
+
104
+ # This module contains the definitions of +serialize+ and +to_serializable+
105
+ # that are included in ActiveRecord::Base.
106
+ module Model
107
+ # :call-seq:
108
+ # serialize format, options = {}, :attributes => [ ... ]
109
+ #
110
+ # See the module documentation for Serialize for details.
111
+ def serialize(format, options)
112
+ raise "Must specify :attributes option" unless options[:attributes]
113
+ hash = self.to_serializable(options[:attributes])
114
+ root = self.class.to_s.underscore
115
+ if format == :xml
116
+ hash.send("to_#{format}", :root => root)
117
+ else
118
+ {root => hash}.send("to_#{format}")
119
+ end
120
+ end
121
+
122
+ # See the module documentation for Serialize for details.
123
+ def to_serializable(attributes)
124
+ raise "Must specify attributes for #{self.inspect}.to_serializable" if attributes.nil?
125
+
126
+ Serialize.normalize_attributes(attributes).inject({}) do |hash, (key, value)|
127
+ hash[key.to_s] = attr_hash_value(self.send(key), value)
128
+ hash
129
+ end
130
+ end
131
+
132
+ private
133
+
134
+ # Given an attribute value
135
+ # and a normalized (see above) attribute hash,
136
+ # returns the serializable form of that attribute.
137
+ def attr_hash_value(attr, sub_attributes)
138
+ if attr.respond_to?(:to_serializable)
139
+ attr.to_serializable(sub_attributes)
140
+ else
141
+ attr
142
+ end
143
+ end
144
+ end
145
+
146
+ # This module contains the definitions of +serialize+ and +to_serializable+
147
+ # that are included in ActiveRecord::Base.
148
+ module Array
149
+ # :call-seq:
150
+ # serialize format, options = {}, :attributes => [ ... ]
151
+ #
152
+ # See the module documentation for Serialize for details.
153
+ def serialize(format, options)
154
+ raise "Not all elements respond to to_serializable" unless all? { |e| e.respond_to? :to_serializable }
155
+ raise "Must specify :attributes option" unless options[:attributes]
156
+
157
+ serialized = map { |e| e.to_serializable(options[:attributes]) }
158
+ root = first.class.to_s.pluralize.underscore
159
+
160
+ if format == :xml
161
+ serialized.send("to_#{format}", :root => root)
162
+ else
163
+ {root => serialized}.send("to_#{format}")
164
+ end
165
+ end
166
+
167
+ # See the module documentation for Serialize for details.
168
+ def to_serializable(attributes)
169
+ if first.respond_to?(:to_serializable)
170
+ attributes = Serialize.normalize_attributes(attributes)
171
+ map { |e| e.to_serializable(attributes) }
172
+ else
173
+ self
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+
180
+ class ActiveRecord::Base; include Resourceful::Serialize::Model; end
181
+ class Array; include Resourceful::Serialize::Array; end