motr-cargo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -3
- data/app/controllers/motr/cargo_contexts_controller.rb +12 -0
- data/app/controllers/motr/cargo_controller.rb +8 -0
- data/lib/motr-cargo.rb +12 -11
- data/lib/motr/cargo/context.rb +72 -0
- data/lib/motr/cargo/context/mongoid.rb +4 -24
- data/lib/motr/cargo/delivery.rb +28 -0
- data/lib/motr/cargo/delivery/actions.rb +39 -0
- data/lib/motr/cargo/delivery/helpers.rb +57 -0
- data/lib/motr/cargo/delivery/templating.rb +6 -0
- data/lib/motr/cargo/package.rb +37 -0
- data/lib/motr/cargo/package/mongoid.rb +4 -7
- data/lib/motr/controller/cargo_helpers.rb +45 -0
- data/lib/motr/mods/cargo.rb +18 -5
- data/lib/motr/rails/cargo_routing.rb +33 -0
- metadata +14 -4
data/Rakefile
CHANGED
@@ -6,8 +6,6 @@ rescue LoadError
|
|
6
6
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
7
|
end
|
8
8
|
|
9
|
-
require 'bundler'
|
10
|
-
Bundler.setup
|
11
9
|
Bundler::GemHelper.install_tasks
|
12
10
|
|
13
11
|
|
@@ -23,7 +21,7 @@ Rake::TestTask.new(:test) do |t|
|
|
23
21
|
t.verbose = false
|
24
22
|
end
|
25
23
|
|
26
|
-
task :default => :test
|
24
|
+
#task :default => :test
|
27
25
|
|
28
26
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
29
27
|
rdoc.rdoc_dir = 'rdoc'
|
data/lib/motr-cargo.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
require 'motr'
|
2
|
-
require 'motr/modding'
|
2
|
+
require 'motr/modding'
|
3
|
+
require 'motr/cargo/package'
|
4
|
+
require 'motr/cargo/context'
|
5
|
+
require 'motr/cargo/delivery'
|
3
6
|
|
4
7
|
module Motr
|
5
8
|
module Cargo
|
6
|
-
VERSION = '0.0.
|
7
|
-
|
8
|
-
module Package
|
9
|
-
autoload :Mongoid, 'motr/cargo/package/mongoid'
|
10
|
-
end
|
11
|
-
|
12
|
-
module Context
|
13
|
-
autoload :Mongoid, 'motr/cargo/context/mongoid'
|
14
|
-
end
|
9
|
+
VERSION = '0.0.2'
|
10
|
+
end
|
15
11
|
|
12
|
+
module Controller
|
13
|
+
autoload :CargoHelpers, 'motr/controller/cargo_helpers'
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
|
-
Motr.add_mod(:cargo, :autoload => true)
|
17
|
+
Motr.add_mod(:cargo, :autoload => true)
|
18
|
+
Motr.apply_helpers(Motr::Controller::CargoHelpers)
|
19
|
+
|
20
|
+
require 'motr/rails/cargo_routing'
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Motr
|
2
|
+
module Cargo
|
3
|
+
##
|
4
|
+
# Defines base functionality for contexts
|
5
|
+
# When applied a package will accept_nested_attributes_for :context_name, and each context
|
6
|
+
# will ensure that, when created, it sets its position to the end of the context list.
|
7
|
+
#
|
8
|
+
module Context
|
9
|
+
|
10
|
+
autoload :Mongoid, 'motr/cargo/context/mongoid'
|
11
|
+
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
included do
|
15
|
+
class_attribute(:cargo_packages, :instance_writer => false) unless self.respond_to?(:cargo_packages)
|
16
|
+
self.cargo_packages ||= []
|
17
|
+
|
18
|
+
mod_opts = cargo_mod_options
|
19
|
+
cname = self.name.to_s.pluralize.underscore
|
20
|
+
orm_const = self.motr_orm_type.to_s.classify
|
21
|
+
packs = [mod_opts[:for]].flatten.compact
|
22
|
+
|
23
|
+
create_mod_attribute!(:name, String)
|
24
|
+
create_mod_attribute!(:body, String)
|
25
|
+
create_mod_attribute!(:meta, Hash)
|
26
|
+
|
27
|
+
raise Motr::Errors::InvalidOptions.new("Missing :for option when defining a Motr::Cargo context") if packs.empty?
|
28
|
+
include Motr::Cargo::Context.const_get(orm_const)
|
29
|
+
|
30
|
+
packs.each do |cargo|
|
31
|
+
send :cargo_context_belongs_to, :"#{cargo}"
|
32
|
+
cargo_class = cargo.to_s.classify.constantize
|
33
|
+
cargo_class.class_eval do
|
34
|
+
|
35
|
+
## Ensure the package module has been initialized
|
36
|
+
self.send(:modded_with, :cargo) unless self.motr_mods.include?(:cargo)
|
37
|
+
self.cargo_contexts << cname
|
38
|
+
|
39
|
+
## Packages shuold always accept nested contexts
|
40
|
+
send :accepts_nested_attributes_for, :"#{cname}", :allow_delete => true
|
41
|
+
send :cargo_context_has_many, :"#{cname}"
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
self.cargo_packages |= packs
|
48
|
+
before_create :ensure_context_position_value
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Ensure new contexts are added to the end of the context list.
|
54
|
+
#
|
55
|
+
def ensure_context_position_value
|
56
|
+
return true unless self.position.nil?
|
57
|
+
self.position = _parent.send(:"#{self.class.name.pluralize.underscore}").count + 1
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# Defines a name to represent the context. When the name attribute is set, it is used, if not
|
62
|
+
# the class name is used.
|
63
|
+
#
|
64
|
+
def context_name
|
65
|
+
(self.name.blank? ? self.class.name : self.name)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
@@ -1,33 +1,13 @@
|
|
1
1
|
module Motr
|
2
2
|
module Cargo::Context
|
3
|
-
|
4
|
-
module Mongoid
|
3
|
+
# @private
|
4
|
+
module Mongoid #:nodoc:
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
included do
|
8
|
-
mod_opts = cargo_mod_options
|
9
|
-
|
10
|
-
field :name, :type => String
|
11
|
-
field :body, :type => String
|
12
|
-
field :meta, :type => Hash
|
13
|
-
|
14
|
-
cname = self.name.to_s.pluralize.underscore
|
15
|
-
packs = [mod_opts[:for]].flatten.compact
|
16
|
-
if packs.empty?
|
17
|
-
raise Motr::Errors::InvalidOptions.new("Missing :for option when defining a Motr::Cargo context")
|
18
|
-
end
|
19
|
-
# Define relationships on each cargo object
|
20
|
-
#
|
21
|
-
packs.flatten.compact.each do |cargo|
|
22
|
-
send(:embedded_in, :"#{cargo.to_s}")
|
23
|
-
cargo.to_s.classify.constantize.class_eval do
|
24
|
-
send(:embeds_many, :"#{cname}")
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
7
|
+
included do
|
28
8
|
end
|
29
9
|
|
30
|
-
end
|
10
|
+
end # end mod
|
31
11
|
|
32
12
|
end
|
33
13
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Motr
|
2
|
+
module Cargo
|
3
|
+
module Delivery
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
autoload :Helpers, 'motr/cargo/delivery/helpers'
|
7
|
+
autoload :Templating, 'motr/cargo/delivery/templating'
|
8
|
+
autoload :Actions, 'motr/cargo/delivery/actions'
|
9
|
+
|
10
|
+
included do
|
11
|
+
include Motr::Cargo::Delivery::Templating
|
12
|
+
include Motr::Cargo::Delivery::Helpers
|
13
|
+
include Motr::Cargo::Delivery::Actions
|
14
|
+
before_filter :authenticate_for_cargo!, :except => [:index, :show]
|
15
|
+
respond_to :html if self.mimes_for_respond_to.empty?
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# Actions for creating/updating/deleting content are more than likely protected.
|
20
|
+
# Overriding this method allows authentication checking to be performed.
|
21
|
+
#
|
22
|
+
def authenticate_for_cargo!
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Motr::Cargo
|
2
|
+
module Delivery
|
3
|
+
|
4
|
+
module Actions
|
5
|
+
|
6
|
+
def index
|
7
|
+
resource = scope_class.all
|
8
|
+
set_instance_var(resource)
|
9
|
+
respond_with(get_instance_var)
|
10
|
+
end
|
11
|
+
|
12
|
+
def show
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def new
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def create
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def edit
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def destroy
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Motr::Cargo
|
2
|
+
module Delivery
|
3
|
+
# @private
|
4
|
+
module Helpers #:nodoc:
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
helper_method :cargo_context_path, :cargo_contexts_path, :cargo_context_url, :cargo_contexts_url,
|
9
|
+
:edit_cargo_context_path, :edit_cargo_context_url, :scope_name, :scope_class
|
10
|
+
end
|
11
|
+
|
12
|
+
def cargo_context_path(pack, context, options = {})
|
13
|
+
motr_cargo_context_path(pack.class.name.to_s, pack.id, context.id, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def cargo_context_url(pack, context, options = {})
|
17
|
+
motr_cargo_context_url(pack.class.name.to_s, pack.id, context.id, options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def cargo_contexts_path(pack, options = {})
|
21
|
+
motr_cargo_contexts_path(pack.scope_type, pack.id, options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def cargo_contexts_url(pack, options = {})
|
25
|
+
motr_cargo_contexts_url(pack.scope_type, pack.id, options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def edit_cargo_context_path(pack, context, options = {})
|
29
|
+
edit_motr_cargo_context_path(pack.class.name.to_s, pack.id, context.id, options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def edit_cargo_context_url(pack, context, options = {})
|
33
|
+
edit_motr_cargo_context_url(pack.class.name.to_s, pack.id, context.id, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def scope_name
|
37
|
+
(action_name.to_s === 'index' ? scope_class.to_s.pluralize : scope_class.to_s).underscore
|
38
|
+
end
|
39
|
+
|
40
|
+
def scope_class
|
41
|
+
(params[:scope_name] || self.class.to_s.split("::").first.gsub(/controller/i, '').singularize).constantize
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def get_instance_var
|
47
|
+
instance_variable_get("@#{scope_name}")
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_instance_var(obj)
|
51
|
+
instance_variable_set("@#{scope_name}", obj)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Motr
|
2
|
+
module Cargo
|
3
|
+
|
4
|
+
module Package
|
5
|
+
|
6
|
+
autoload :Mongoid, 'motr/cargo/package/mongoid'
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
class_attribute(:cargo_contexts, :instance_writer => false) unless self.respond_to?(:cargo_contexts)
|
12
|
+
self.cargo_contexts ||= []
|
13
|
+
orm_const = self.motr_orm_type.to_s.classify
|
14
|
+
send(:include, Motr::Cargo::Package.const_get(orm_const))
|
15
|
+
create_mod_attribute!(:title, String)
|
16
|
+
create_mod_attribute!(:post_date, Date)
|
17
|
+
create_mod_attribute!(:published, Boolean)
|
18
|
+
attr_accessor :cargo
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# The cargo method handles "rendering" the particular package.
|
23
|
+
# This data should be defined in the cargo "show" action.
|
24
|
+
#
|
25
|
+
def cargo=(cargo_data)
|
26
|
+
@cargo
|
27
|
+
end
|
28
|
+
|
29
|
+
def cargo
|
30
|
+
@cargo
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -1,15 +1,12 @@
|
|
1
1
|
module Motr
|
2
2
|
module Cargo::Package
|
3
|
-
|
4
|
-
module Mongoid
|
3
|
+
# @private
|
4
|
+
module Mongoid #:nodoc:
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
included do
|
8
|
-
|
9
|
-
field :
|
10
|
-
field :published, :type => Boolean
|
11
|
-
field :uid, :type => Integer
|
12
|
-
|
8
|
+
include ::Mongoid::MultiParameterAttributes
|
9
|
+
field :uid, :type => Integer
|
13
10
|
before_save :generate_cargo_uid
|
14
11
|
end
|
15
12
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Motr
|
2
|
+
module Controller
|
3
|
+
|
4
|
+
module CargoHelpers
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :motr_cargo_classes, :instance_writer => false
|
9
|
+
self.motr_cargo_classes ||= []
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
##
|
14
|
+
# Configures models a particular controller "delivers".
|
15
|
+
# By default cargo considers the controller name to be the resource name
|
16
|
+
# that it is to serve. Calling delivers_cargo_for allows the controller to
|
17
|
+
# find objects based on object_id parameters.
|
18
|
+
#
|
19
|
+
# @param [Symbol,Array] *resources The resources/models this controller delivers
|
20
|
+
#
|
21
|
+
# @example Configure a Posts controller to deliver resources for Blog and Article
|
22
|
+
# class PostsController < Motr::CargosController
|
23
|
+
# delivers_cargo_for :blog, :article
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
def delivers_cargo_for(*resources)
|
27
|
+
options = resources.extract_options!
|
28
|
+
self.motr_cargo_classes |= resources
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Normally you would have your controllers extend Motr::CargoController,
|
33
|
+
# in the event this isn't possible, calling delivers_cargo will ensure
|
34
|
+
# the proper functionality exists.
|
35
|
+
#
|
36
|
+
def delivers_cargo
|
37
|
+
include Motr::Cargo::Delivery
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
data/lib/motr/mods/cargo.rb
CHANGED
@@ -38,13 +38,26 @@ module Motr
|
|
38
38
|
|
39
39
|
options(:for, :as)
|
40
40
|
|
41
|
-
configure
|
41
|
+
configure do
|
42
42
|
mod_opts = cargo_mod_options
|
43
|
-
unless mod_opts.key?(:as) && mod_opts[:as].to_s.match(/context/i)
|
44
|
-
|
43
|
+
unless mod_opts.key?(:as) && mod_opts[:as].to_s.match(/context/i)
|
44
|
+
include Motr::Cargo::Package
|
45
45
|
else
|
46
|
-
|
47
|
-
|
46
|
+
include Motr::Cargo::Context
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
configure(:mongoid) do
|
51
|
+
class << self
|
52
|
+
alias :cargo_context_belongs_to :embedded_in
|
53
|
+
alias :cargo_context_has_many :embeds_many
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
configure(:active_record) do
|
58
|
+
class << self
|
59
|
+
alias :cargo_context_belongs_to :belongs_to
|
60
|
+
alias :cargo_context_has_many :has_many
|
48
61
|
end
|
49
62
|
end
|
50
63
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ActionDispatch::Routing
|
2
|
+
class Mapper
|
3
|
+
attr_accessor :cargo_context_routes_set
|
4
|
+
##
|
5
|
+
# Calling deliver_cargo_for within routes sets up routing for each
|
6
|
+
# cargo model. By default all passed models are routed with:
|
7
|
+
# :controller => 'motr/cargo
|
8
|
+
# :scope_name => 'ModelName'
|
9
|
+
#
|
10
|
+
# Override these options when configuring if you would prefer a different controller/scope name
|
11
|
+
#
|
12
|
+
def deliver_cargo_for(*models)
|
13
|
+
|
14
|
+
options = models.extract_options!
|
15
|
+
models.map!(&:to_sym).each do |mod|
|
16
|
+
sing = ActiveSupport::Inflector.singularize(mod.to_s)
|
17
|
+
plural = ActiveSupport::Inflector.pluralize(mod.to_s)
|
18
|
+
resources *[plural, options.reverse_merge(:controller => 'motr/cargo', :scope_name => "#{sing.classify}")]
|
19
|
+
end
|
20
|
+
|
21
|
+
unless @cargo_context_routes_set
|
22
|
+
scope :controller => 'motr/cargo_contexts' do
|
23
|
+
get 'motr/cargo/:scope_name/:scope_id/contexts', :action => :index, :as => :motr_cargo_contexts
|
24
|
+
get 'motr/cargo/:scope_name/:scope_id/contexts/:id/edit', :action => :edit, :as => :edit_motr_cargo_context
|
25
|
+
put 'motr/cargo/:scope_name/:scope_id/contexts/:id', :action => :update, :as => :motr_cargo_context
|
26
|
+
delete 'motr/cargo/:scope_name/:scope_id/contexts/:id', :action => :destroy, :as => :motr_cargo_context
|
27
|
+
end
|
28
|
+
@cargo_context_routes_set = true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: motr-cargo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brent Kirby
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-24 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -144,9 +144,19 @@ extensions: []
|
|
144
144
|
extra_rdoc_files: []
|
145
145
|
|
146
146
|
files:
|
147
|
+
- app/controllers/motr/cargo_contexts_controller.rb
|
148
|
+
- app/controllers/motr/cargo_controller.rb
|
147
149
|
- lib/motr/cargo/context/mongoid.rb
|
150
|
+
- lib/motr/cargo/context.rb
|
151
|
+
- lib/motr/cargo/delivery/actions.rb
|
152
|
+
- lib/motr/cargo/delivery/helpers.rb
|
153
|
+
- lib/motr/cargo/delivery/templating.rb
|
154
|
+
- lib/motr/cargo/delivery.rb
|
148
155
|
- lib/motr/cargo/package/mongoid.rb
|
156
|
+
- lib/motr/cargo/package.rb
|
157
|
+
- lib/motr/controller/cargo_helpers.rb
|
149
158
|
- lib/motr/mods/cargo.rb
|
159
|
+
- lib/motr/rails/cargo_routing.rb
|
150
160
|
- lib/motr-cargo.rb
|
151
161
|
- MIT-LICENSE
|
152
162
|
- Rakefile
|
@@ -166,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
166
176
|
requirements:
|
167
177
|
- - ">="
|
168
178
|
- !ruby/object:Gem::Version
|
169
|
-
hash:
|
179
|
+
hash: 2143364796700752731
|
170
180
|
segments:
|
171
181
|
- 0
|
172
182
|
version: "0"
|
@@ -175,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
185
|
requirements:
|
176
186
|
- - ">="
|
177
187
|
- !ruby/object:Gem::Version
|
178
|
-
hash:
|
188
|
+
hash: 2143364796700752731
|
179
189
|
segments:
|
180
190
|
- 0
|
181
191
|
version: "0"
|