easy_admin_ui 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +4 -15
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/{assets/icons → app/assets/images/easy_admin_ui}/classify.png +0 -0
- data/{assets/icons → app/assets/images/easy_admin_ui}/connect.png +0 -0
- data/{assets/icons → app/assets/images/easy_admin_ui}/delete.png +0 -0
- data/{assets/icons → app/assets/images/easy_admin_ui}/disconnect.png +0 -0
- data/{assets/icons → app/assets/images/easy_admin_ui}/pencil.png +0 -0
- data/{assets/icons → app/assets/images/easy_admin_ui}/show.png +0 -0
- data/app/assets/stylesheets/easy_admin_ui/easy_admin_ui.sass +47 -0
- data/app/views/easy_admin_ui/destroy.js.erb +1 -0
- data/{templates → app/views}/easy_admin_ui/edit.html.erb +0 -0
- data/{templates → app/views}/easy_admin_ui/index.html.erb +0 -0
- data/{templates → app/views}/easy_admin_ui/new.html.erb +0 -0
- data/{templates → app/views}/easy_admin_ui/show.html.erb +0 -0
- data/easy_admin_ui.gemspec +80 -21
- data/lib/easy_admin_ui/core_ext.rb +1 -3
- data/lib/easy_admin_ui/rails/engine.rb +10 -0
- data/lib/easy_admin_ui/tasks.rb +0 -10
- data/lib/easy_admin_ui/view_helpers.rb +3 -3
- data/lib/easy_admin_ui.rb +1 -9
- data/vendor/plugins/make_resourceful/.gitignore +1 -0
- data/vendor/plugins/make_resourceful/DEFAULTS +148 -0
- data/vendor/plugins/make_resourceful/LICENSE +7 -0
- data/vendor/plugins/make_resourceful/README.rdoc +239 -0
- data/vendor/plugins/make_resourceful/Rakefile +31 -0
- data/vendor/plugins/make_resourceful/VERSION +1 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/resourceful_scaffold_generator.rb +87 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/controller.rb +5 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/fixtures.yml +10 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/functional_test.rb +50 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/helper.rb +2 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/migration.rb +13 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/model.rb +2 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/unit_test.rb +7 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view__form.haml +5 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_edit.haml +11 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_index.haml +5 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_new.haml +9 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_partial.haml +12 -0
- data/vendor/plugins/make_resourceful/generators/resourceful_scaffold/templates/view_show.haml +14 -0
- data/vendor/plugins/make_resourceful/init.rb +3 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/base.rb +63 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/builder.rb +385 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/default/accessors.rb +402 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/default/actions.rb +101 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/default/callbacks.rb +51 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/default/responses.rb +118 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/default/urls.rb +137 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/maker.rb +84 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/response.rb +33 -0
- data/vendor/plugins/make_resourceful/lib/resourceful/serialize.rb +185 -0
- data/vendor/plugins/make_resourceful/spec/accessors_spec.rb +474 -0
- data/vendor/plugins/make_resourceful/spec/actions_spec.rb +310 -0
- data/vendor/plugins/make_resourceful/spec/base_spec.rb +12 -0
- data/vendor/plugins/make_resourceful/spec/builder_spec.rb +332 -0
- data/vendor/plugins/make_resourceful/spec/callbacks_spec.rb +71 -0
- data/vendor/plugins/make_resourceful/spec/integration_spec.rb +394 -0
- data/vendor/plugins/make_resourceful/spec/maker_spec.rb +91 -0
- data/vendor/plugins/make_resourceful/spec/response_spec.rb +37 -0
- data/vendor/plugins/make_resourceful/spec/responses_spec.rb +314 -0
- data/vendor/plugins/make_resourceful/spec/rspec-rails/LICENSE +37 -0
- data/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb +113 -0
- data/vendor/plugins/make_resourceful/spec/rspec-rails/render_template.rb +90 -0
- data/vendor/plugins/make_resourceful/spec/serialize_spec.rb +133 -0
- data/vendor/plugins/make_resourceful/spec/spec_helper.rb +319 -0
- data/vendor/plugins/make_resourceful/spec/urls_spec.rb +282 -0
- data/vendor/plugins/make_resourceful/spec/views/things/create.rjs +1 -0
- data/vendor/plugins/make_resourceful/spec/views/things/destroy.rjs +1 -0
- data/vendor/plugins/make_resourceful/spec/views/things/edit.html.erb +4 -0
- data/vendor/plugins/make_resourceful/spec/views/things/edit.rjs +1 -0
- data/vendor/plugins/make_resourceful/spec/views/things/index.html.erb +4 -0
- data/vendor/plugins/make_resourceful/spec/views/things/index.rjs +1 -0
- data/vendor/plugins/make_resourceful/spec/views/things/new.html.erb +4 -0
- data/vendor/plugins/make_resourceful/spec/views/things/new.rjs +1 -0
- data/vendor/plugins/make_resourceful/spec/views/things/show.html.erb +4 -0
- data/vendor/plugins/make_resourceful/spec/views/things/show.rjs +1 -0
- data/vendor/plugins/make_resourceful/spec/views/things/update.rjs +1 -0
- metadata +75 -16
- data/assets/css/easy_admin_ui.css +0 -69
@@ -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_class_name.underscore, 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_class_name.underscore, 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_class_name.underscore}_" : ''
|
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_class_name.underscore}_#{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}#{collection_url_prefix}#{name}_#{type}", *(parent? ? [parent_object, object] : [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,84 @@
|
|
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
|
+
:nested_object_path, :nested_object_url,
|
79
|
+
:current_objects, :current_object, :current_model, :current_model_name,
|
80
|
+
:namespaces, :instance_variable_name, :parent_names, :parent_name,
|
81
|
+
:parent?, :parent_model, :parent_object, :save_succeeded?)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
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,185 @@
|
|
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
|
+
if defined? ActiveModel
|
181
|
+
class ActiveModel::Base; include Resourceful::Serialize::Model; end
|
182
|
+
else
|
183
|
+
class ActiveRecord::Base; include Resourceful::Serialize::Model; end
|
184
|
+
end
|
185
|
+
class Array; include Resourceful::Serialize::Array; end
|