motr 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,11 @@ require 'motr/model'
7
7
 
8
8
  module Motr
9
9
 
10
+ module Schema
11
+ autoload :Base, 'motr/schema/base'
12
+ autoload :Generator, 'motr/schema/generator'
13
+ end
14
+
10
15
  module Controller #:nodoc:
11
16
  autoload :Helpers, 'motr/controller/helpers'
12
17
  autoload :Responder, 'motr/controller/responder'
@@ -25,7 +25,7 @@ module Motr
25
25
  # nav_link_to('Current Page', '/current_url') #=> <li class="on"><a href="/current_url" class="on">Current Page</a></li>
26
26
  #
27
27
  def nav_link_to(text, path, attrs = {}, wrapper = :li)
28
-
28
+
29
29
  link_attrs = update_link_attrs(path, attrs)
30
30
  wrapper_attrs = link_attrs.delete(:wrapper)
31
31
  child_link = link_to(text, path, link_attrs)
@@ -85,10 +85,7 @@ module Motr
85
85
  klasses ||= []
86
86
  wklasses ||= []
87
87
 
88
- if proc && proc.call(path)
89
- klasses << on_class
90
- wklasses << on_class
91
- elsif regex && path.match(regex)
88
+ if ((proc && proc.call(path)) || (regex && request.path =~ regex) || (request.path == path || request.path.match(/#{path}\/.*/i)))
92
89
  klasses << on_class
93
90
  wklasses << on_class
94
91
  end
@@ -76,6 +76,12 @@ module Motr
76
76
  options = mods.extract_options!
77
77
  modules = mods.map(&:to_sym).uniq
78
78
 
79
+ # Track what modules have been added to each class.
80
+ self.class_eval do
81
+ class_attribute :motr_modules, :instance_writer => false unless defined?(self.motr_modules)
82
+ self.motr_modules ||= []
83
+ end
84
+
79
85
  motrize_model! do
80
86
 
81
87
  modules.each do |mod|
@@ -104,7 +110,11 @@ module Motr
104
110
  include const_incl
105
111
 
106
112
  end
113
+
114
+ self.motr_modules |= modules
115
+
107
116
  end
117
+
108
118
  end
109
119
 
110
120
  # Raise a ModuleMissing error if a module cannot be loaded.
@@ -8,9 +8,15 @@ module Motr
8
8
  extend Motr::Model
9
9
  end
10
10
 
11
+ module Schema
12
+ include Motr::Schema::Base
13
+ end
14
+
11
15
  end
12
16
 
13
17
  end
14
18
  end
15
19
 
16
- ActiveRecord::Base.send :include, Motr::Orm::ActiveRecord
20
+ ActiveRecord::Base.send :include, Motr::Orm::ActiveRecord
21
+ # ActiveRecord::ConnectionAdapters::Table.send :include, Motr::Orm::ActiveRecord::Schema
22
+ # ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Motr::Orm::ActiveRecord::Schema
@@ -2,6 +2,22 @@ module Motr
2
2
  module Orm
3
3
 
4
4
  module Mongoid #:nodoc:
5
+ def motrize_model!
6
+ extend Schema
7
+ yield
8
+ motrize_schema!
9
+ end
10
+
11
+ module Schema
12
+ include Motr::Schema::Base
13
+
14
+ def apply_motr_schema(field_name, field_type, options = {})
15
+ field_type = Time if field_type == DateTime
16
+ field field_name, { :type => field_type }.merge!(options)
17
+ end
18
+
19
+ end
20
+
5
21
  end
6
22
 
7
23
  end
@@ -18,6 +18,10 @@ module Motr
18
18
  include Motr::Controller::Helpers
19
19
  self.responder = Motr::Controller::Responder
20
20
  end
21
+ ActiveSupport.on_load(:after_initialize) do
22
+ require 'motr/orm/mongoid' if defined?(Mongoid)
23
+ require 'motr/orm/active_record' if defined?(ActiveRecord)
24
+ end
21
25
 
22
26
  end
23
27
 
@@ -0,0 +1,20 @@
1
+ module Motr
2
+ module Schema
3
+
4
+ ##
5
+ # Stubs schema methods to ensure ORM's implement them
6
+ # @private
7
+ #
8
+ module Base #:nodoc:
9
+
10
+ def motrize_schema!
11
+ Motr::Schema::Generator.set_target(self)
12
+ end
13
+
14
+ def apply_motr_schema(name, type, options={})
15
+ raise NotImplementedError
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,79 @@
1
+ module Motr
2
+ module Schema
3
+ ##
4
+ # Generates a schema for a Motr module. To define a module schema, create a
5
+ # subclass that implements a "generate" class method. Generator classes
6
+ # should follow the convention Motr::Schema::ModuleGenerator where "Module" is the module the
7
+ # schema applies to. For instance if you have a module: Motr::Model::Sample, your generator class
8
+ # would be called Motr::Schema::SampleGenerator
9
+ #
10
+ # @example A generator for the module "sample"
11
+ #
12
+ # class Motr::Schema::SampleGenerator < Motr::Schema::Generator
13
+ # def self.generate
14
+ # apply(:field_name, String, {}) # => creates the field "field_name" with the type of String
15
+ #
16
+ # # only run for mongoid models
17
+ # apply_for(:mongoid) do
18
+ # embeds_many :children
19
+ # end
20
+ # end
21
+ # end
22
+ #
23
+ #
24
+ class Generator
25
+
26
+ # Make er clean
27
+ instance_methods.each { |m| undef_method m unless m =~ /^(__.*__|object_id)$/ }
28
+
29
+ class_attribute :schema_runner, :instance_writer => false
30
+
31
+ # @private
32
+ # Delegates callbacks to a particular class/module. This allows generators to be flexible and
33
+ # somewhat similar to migrations.
34
+ #
35
+ def self.set_target(target) #:nodoc:
36
+ self.schema_runner = target
37
+ end
38
+
39
+ # Override in subclasses to run.
40
+ #
41
+ def self.generate
42
+ raise NotImplementedError
43
+ end
44
+
45
+ protected
46
+
47
+ ##
48
+ # Apply a callback only if the including module implements the specified ORM
49
+ #
50
+ # @param [Symbol] orm The ORM which handles the block (ie: :mongoid or :active_record)
51
+ # @param [Block] &block The block to be run.
52
+ #
53
+ # @example Apply an option only to mongoid based models
54
+ # apply_for(:mongoid) do |model|
55
+ # model.embeds_many :children
56
+ # end
57
+ #
58
+ def self.apply_for(orm, &block)
59
+ # TODO: This should maybe be more efficient?
60
+ puts self.schema_runner.inspect
61
+ return unless self.schema_runner.ancestors.map(&:to_s).detect{ |a| a.to_s.match(/#{orm.to_s.classify}/) }
62
+ yield self.schema_runner
63
+ end
64
+
65
+ ##
66
+ # Creates a field on the model (or migration method in ActiveRecord)
67
+ #
68
+ # @param [Symbol] name The name of the field to create
69
+ # @param [Class] type The class type to create (ie: String, DateTime...)
70
+ # @param [Hash] options Specific options for the class type (ie: :default => nil)
71
+ #
72
+ def self.apply(name, type, options = {})
73
+ self.schema_runner.__send__(:apply_motr_schema, name, type, options)
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end
@@ -1,3 +1,3 @@
1
1
  module Motr #:nodoc:
2
- VERSION = "0.0.4".freeze
2
+ VERSION = "0.0.5".freeze
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: motr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
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-09 00:00:00 -04:00
13
+ date: 2011-05-11 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -113,16 +113,16 @@ extra_rdoc_files: []
113
113
  files:
114
114
  - app/helpers/motr_helper.rb
115
115
  - lib/config/locales/en.yml
116
- - lib/generator/motr/compass_generator.rb
117
- - lib/generator/motr/install_generator.rb
118
- - lib/generator/motr/motr_generator.rb
119
- - lib/generator/templates/compass/ie.scss
120
- - lib/generator/templates/compass/includes/_base.scss
121
- - lib/generator/templates/compass/includes/_defaults.scss
122
- - lib/generator/templates/compass/includes/_mixins.scss
123
- - lib/generator/templates/compass/screen.scss
124
- - lib/generator/templates/motr.rb
125
- - lib/generator/templates/post_install
116
+ - lib/generators/motr/compass_generator.rb
117
+ - lib/generators/motr/install_generator.rb
118
+ - lib/generators/motr/motr_generator.rb
119
+ - lib/generators/templates/compass/ie.scss
120
+ - lib/generators/templates/compass/includes/_base.scss
121
+ - lib/generators/templates/compass/includes/_defaults.scss
122
+ - lib/generators/templates/compass/includes/_mixins.scss
123
+ - lib/generators/templates/compass/screen.scss
124
+ - lib/generators/templates/motr.rb
125
+ - lib/generators/templates/post_install
126
126
  - lib/motr/controller/helpers.rb
127
127
  - lib/motr/controller/responder.rb
128
128
  - lib/motr/defaults.rb
@@ -141,6 +141,8 @@ files:
141
141
  - lib/motr/orm/active_record.rb
142
142
  - lib/motr/orm/mongoid.rb
143
143
  - lib/motr/rails.rb
144
+ - lib/motr/schema/base.rb
145
+ - lib/motr/schema/generator.rb
144
146
  - lib/motr/version.rb
145
147
  - lib/motr.rb
146
148
  - config/en.yml
@@ -162,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
164
  requirements:
163
165
  - - ">="
164
166
  - !ruby/object:Gem::Version
165
- hash: 2672633965524668472
167
+ hash: -3118347757184532695
166
168
  segments:
167
169
  - 0
168
170
  version: "0"