model_manage 0.1.4 → 0.1.6

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.6
@@ -1,5 +1,3 @@
1
- if defined?(ActiveRecord::Base)
2
-
3
1
  module ActiveRecord
4
2
  class Base
5
3
  def self.inherited(child)
@@ -11,5 +9,3 @@ module ActiveRecord
11
9
  end
12
10
  end
13
11
  end
14
-
15
- end
@@ -0,0 +1,7 @@
1
+ module ActiveRecord
2
+ class Base
3
+ def self.descendants
4
+ Rails.models
5
+ end
6
+ end
7
+ end
@@ -2,23 +2,25 @@ module ModelManage
2
2
  module Base
3
3
  def self.included(base)
4
4
  base.class_eval do
5
+ extend ModelManage::ClassMethods
6
+ forms_field = superclass.forms.dup rescue {}.with_indifferent_access
7
+ base.const_set :FORMS, forms_field
5
8
  end
6
-
7
- def base.forms
9
+ end
10
+ end
11
+ module ClassMethods
12
+ def forms
8
13
  const_get(:FORMS)
9
- end
14
+ end
10
15
 
11
- def base.key?(key)
16
+ def key?(key)
12
17
  const_get(:KEYS).member? key.to_s
13
- rescue
18
+ rescue
14
19
  const_set :KEYS, []
15
20
  false
16
- end
17
-
18
- forms_field = base.superclass.forms.dup rescue {}.with_indifferent_access
19
- base.const_set :FORMS, forms_field
21
+ end
20
22
 
21
- def base.field(name, options = {})
23
+ def field(name, options = {})
22
24
  min = 1
23
25
 
24
26
  validates = {}
@@ -49,9 +51,9 @@ module ModelManage
49
51
  end
50
52
 
51
53
  forms[name] = ::OpenStruct.new(form_attributes)
52
- end
54
+ end
53
55
 
54
- def base.relation_form_set(name, options = {})
56
+ def relation_form_set(name, options = {})
55
57
  relation_attributes = {
56
58
  owner: self,
57
59
  name: name.to_s,
@@ -59,7 +61,6 @@ module ModelManage
59
61
  }.tap{|o| o[:data] = o.dup }
60
62
  relation = relations[name.to_s]
61
63
  relation.form = OpenStruct.new(relation_attributes)
62
- end
63
64
  end
64
65
  end
65
66
  end
@@ -0,0 +1,17 @@
1
+ module ModelManage
2
+ module Mongoid
3
+ module ClassMethods
4
+ def column_for_attribute(attribute_name)
5
+ self.class.forms[attribute_name.to_s]
6
+ end
7
+
8
+ def content_columns
9
+ forms.values
10
+ end
11
+
12
+ def reflections
13
+ relations
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,9 +1,3 @@
1
- if defined?(Mongoid::Document)
2
-
3
- class Mongoid::Relations::Metadata
4
- attr_accessor :form
5
- end
6
-
7
1
  module Mongoid
8
2
  module Document
9
3
  def self.included(base)
@@ -15,132 +9,44 @@ module Mongoid
15
9
  end
16
10
 
17
11
  base.class_eval do
12
+ extend ModelManage::Mongoid::ClassMethods
18
13
  include ModelManage::Base
19
- include ModelManage::Mongoid
20
14
  end
21
15
  end
22
16
  end
23
17
  end
24
18
 
19
+ class Mongoid::Relations::Metadata
20
+ attr_accessor :form
21
+ end
25
22
 
26
23
  module ModelManage
27
24
  module Mongoid
28
- def self.included(base)
29
- def base.key(* keys)
25
+ module ClassMethods
26
+ def key(* keys)
30
27
  const_set :KEYS, keys.map(&:to_s)
31
28
  super
32
29
  end
33
- def base.referenced_in(name, options = {})
30
+ def referenced_in(name, options = {})
34
31
  super
35
32
  relation_form_set name, options
36
33
  end
37
- def base.references_many(name, options = {})
34
+ def references_many(name, options = {})
38
35
  super
39
36
  relation_form_set name, options
40
37
  end
41
- def base.embedded_in(name, options = {})
38
+ def embedded_in(name, options = {})
42
39
  super
43
40
  relation_form_set name, options
44
41
  end
45
- def base.embeds_many(name, options = {})
42
+ def embeds_many(name, options = {})
46
43
  super
47
44
  relation_form_set name, options
48
45
  end
49
- def base.references_and_referenced_in_many(name, options = {})
46
+ def references_and_referenced_in_many(name, options = {})
50
47
  super
51
48
  relation_form_set name, options
52
49
  end
53
-
54
- if defined?(SimpleForm) || defined?(Formtastic)
55
- base.class_eval do
56
- def column_for_attribute(attribute_name)
57
- self.class.forms[attribute_name.to_s]
58
- end
59
- end
60
- end
61
- if defined?(Formtastic)
62
- def base.content_columns
63
- forms.values
64
- end
65
-
66
- def base.reflections
67
- relations
68
- end
69
- end
70
- if defined?(RailsERD)
71
- def base.inheritance_column
72
- '_type'
73
- end
74
-
75
- def base.columns
76
- forms.values
77
- end
78
-
79
- def base.columns_hash
80
- forms
81
- end
82
-
83
- def base.descends_from_active_record?
84
- Rails.child_models.member? self
85
- end
86
-
87
- def base.reflect_on_all_associations(macro = nil)
88
- association_reflections = relations.values
89
- macro ? relations.select { |reflection| reflection.macro == macro } : association_reflections
90
- end
91
-
92
- def base.base_class
93
- class_of_active_record_descendant(self)
94
- end
95
-
96
- def base.class_of_active_record_descendant(klass)
97
- if not Rails.models.member? klass.superclass
98
- klass
99
- else
100
- class_of_active_record_descendant(klass.superclass)
101
- end
102
- end
103
- end
104
- end
105
- end
106
- end
107
-
108
-
109
- if defined?(RailsERD)
110
- module ModelManage
111
- module Metadata
112
- def options
113
- form.data.merge(self)
114
- end
115
- def active_record
116
- form.owner
117
- end
118
- def check_validity!
119
- nil
120
- end
121
- def belongs_to?
122
- [:referenced_in, :embedded_in].member? macro
123
- end
124
- def collection?
125
- not belongs_to?
126
- end
127
- def through_reflection
128
- active_record.relations[ form.options[:through].to_s ]
129
- end
130
- end
131
- end
132
-
133
- class Mongoid::Relations::Metadata
134
- include ModelManage::Metadata
135
- end
136
-
137
- module ActiveRecord
138
- class Base
139
- def self.descendants
140
- Rails.models
141
- end
142
50
  end
143
51
  end
144
- end
145
-
146
52
  end
@@ -0,0 +1,63 @@
1
+ module ModelManage
2
+ module Metadata
3
+ def options
4
+ form.data.merge(self)
5
+ end
6
+ def active_record
7
+ form.owner
8
+ end
9
+ def check_validity!
10
+ nil
11
+ end
12
+ def belongs_to?
13
+ [:referenced_in, :embedded_in].member? macro
14
+ end
15
+ def collection?
16
+ not belongs_to?
17
+ end
18
+ def through_reflection
19
+ active_record.relations[ form.options[:through].to_s ]
20
+ end
21
+ end
22
+
23
+ module Mongoid
24
+ module ClassMethods
25
+ def inheritance_column
26
+ '_type'
27
+ end
28
+
29
+ def columns
30
+ forms.values
31
+ end
32
+
33
+ def columns_hash
34
+ forms
35
+ end
36
+
37
+ def descends_from_active_record?
38
+ Rails.child_models.member? self
39
+ end
40
+
41
+ def reflect_on_all_associations(macro = nil)
42
+ association_reflections = relations.values
43
+ macro ? relations.select { |reflection| reflection.macro == macro } : association_reflections
44
+ end
45
+
46
+ def base_class
47
+ class_of_active_record_descendant(self)
48
+ end
49
+
50
+ def class_of_active_record_descendant(klass)
51
+ if not Rails.models.member? klass.superclass
52
+ klass
53
+ else
54
+ class_of_active_record_descendant(klass.superclass)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ class Mongoid::Relations::Metadata
62
+ include ModelManage::Metadata
63
+ end
data/lib/model_manage.rb CHANGED
@@ -1,6 +1,15 @@
1
1
  require 'ostruct'
2
- require 'model_manage/mongoid'
3
- require 'model_manage/active_record'
2
+
3
+ if defined?(Mongoid::Document)
4
+ require 'model_manage/form' if defined?(SimpleForm) || defined?(Formtastic)
5
+ require 'model_manage/mongoid_rails_erd' if defined?(RailsERD)
6
+ require 'model_manage/mongoid'
7
+ end
8
+
9
+ if defined?(ActiveRecord::Base)
10
+ require 'model_manage/active_record_rails_erd' if defined?(RailsERD)
11
+ require 'model_manage/active_record'
12
+ end
4
13
 
5
14
  require 'model_manage/rails'
6
15
  require 'model_manage/bitfield'
data/model_manage.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "model_manage"
8
- s.version = "0.1.4"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["\u{306a}\u{306a}\u{3053}\u{308d}\u{3073}"]
12
- s.date = "2012-01-01"
12
+ s.date = "2012-01-17"
13
13
  s.description = "\u{30e2}\u{30c7}\u{30eb}\u{3001}\u{30d5}\u{30a3}\u{30fc}\u{30eb}\u{30c9}\u{3068}\u{3044}\u{3063}\u{305f}\u{60c5}\u{5831}\u{3078}\u{7c21}\u{5358}\u{30a2}\u{30af}\u{30bb}\u{30b9}\u{3059}\u{308b}\u{3053}\u{3068}\u{3092}\u{76ee}\u{7684}\u{306b}\u{958b}\u{767a}\u{3057}\u{3066}\u{3044}\u{307e}\u{3059}\u{3002}"
14
14
  s.email = "7korobi@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -26,11 +26,12 @@ Gem::Specification.new do |s|
26
26
  "lib/.DS_Store",
27
27
  "lib/model_manage.rb",
28
28
  "lib/model_manage/active_record.rb",
29
+ "lib/model_manage/active_record_rails_erd.rb",
29
30
  "lib/model_manage/base.rb",
30
31
  "lib/model_manage/bitfield.rb",
31
- "lib/model_manage/formtastic/array_input.rb",
32
- "lib/model_manage/formtastic/input_helper.rb",
32
+ "lib/model_manage/form.rb",
33
33
  "lib/model_manage/mongoid.rb",
34
+ "lib/model_manage/mongoid_rails_erd.rb",
34
35
  "lib/model_manage/rails.rb",
35
36
  "model_manage.gemspec",
36
37
  "test/helper.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_manage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-01 00:00:00.000000000 Z
12
+ date: 2012-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: simple_form
16
- requirement: &70120811590500 !ruby/object:Gem::Requirement
16
+ requirement: &70242343742300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70120811590500
24
+ version_requirements: *70242343742300
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongoid
27
- requirement: &70120811589420 !ruby/object:Gem::Requirement
27
+ requirement: &70242343739620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70120811589420
35
+ version_requirements: *70242343739620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &70120811588720 !ruby/object:Gem::Requirement
38
+ requirement: &70242343732720 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70120811588720
46
+ version_requirements: *70242343732720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &70120811588040 !ruby/object:Gem::Requirement
49
+ requirement: &70242343731720 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.6.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70120811588040
57
+ version_requirements: *70242343731720
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rcov
60
- requirement: &70120811587180 !ruby/object:Gem::Requirement
60
+ requirement: &70242343729980 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70120811587180
68
+ version_requirements: *70242343729980
69
69
  description: モデル、フィールドといった情報へ簡単アクセスすることを目的に開発しています。
70
70
  email: 7korobi@gmail.com
71
71
  executables: []
@@ -83,11 +83,12 @@ files:
83
83
  - lib/.DS_Store
84
84
  - lib/model_manage.rb
85
85
  - lib/model_manage/active_record.rb
86
+ - lib/model_manage/active_record_rails_erd.rb
86
87
  - lib/model_manage/base.rb
87
88
  - lib/model_manage/bitfield.rb
88
- - lib/model_manage/formtastic/array_input.rb
89
- - lib/model_manage/formtastic/input_helper.rb
89
+ - lib/model_manage/form.rb
90
90
  - lib/model_manage/mongoid.rb
91
+ - lib/model_manage/mongoid_rails_erd.rb
91
92
  - lib/model_manage/rails.rb
92
93
  - model_manage.gemspec
93
94
  - test/helper.rb
@@ -107,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
108
  version: '0'
108
109
  segments:
109
110
  - 0
110
- hash: -3390306210837111882
111
+ hash: -3860435876427644110
111
112
  required_rubygems_version: !ruby/object:Gem::Requirement
112
113
  none: false
113
114
  requirements:
@@ -1,17 +0,0 @@
1
- module Formtastic
2
- module Inputs
3
-
4
- class ArrayInput
5
- include Base
6
- include Base::Stringish
7
-
8
- def to_html
9
- @object.each do |exp|
10
- input_wrapping do
11
- label_html <<
12
- builder.search_field(method, input_html_options)
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,105 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module Formtastic
3
- module Helpers
4
- module InputHelper
5
-
6
- def input(method, options = {})
7
- options = options.dup # Allow options to be shared without being tainted by Formtastic
8
- options[:as] ||= default_input_type(method, options)
9
-
10
- klass = input_class(options[:as])
11
- klass.new(self, template, @object, @object_name, method, options).to_html
12
- end
13
-
14
- protected
15
-
16
- def default_input_type(method, options = {}) #:nodoc:
17
- if @object
18
- return :select if reflection_for(method)
19
-
20
- return :file if is_file?(method, options)
21
- end
22
-
23
- if column = column_for(method)
24
- # Special cases where the column type doesn't map to an input method.
25
- case column.type
26
- when String, :string
27
- return :password if method.to_s =~ /password/
28
- return :country if method.to_s =~ /country$/
29
- return :time_zone if method.to_s =~ /time_zone/
30
- return :email if method.to_s =~ /email/
31
- return :url if method.to_s =~ /^url$|^website$|_url$/
32
- return :phone if method.to_s =~ /(phone|fax)/
33
- return :search if method.to_s =~ /^search$/
34
- when Integer, :integer
35
- return :select if reflection_for(method)
36
- return :number
37
- when Float, Decimal, :float, :decimal
38
- return :number
39
- when Time, :timestamp
40
- return :string
41
- when Array
42
- return :array
43
- end
44
-
45
- # Try look for hints in options hash. Quite common senario: Enum keys stored as string in the database.
46
- return :select if column.type == :string && options.key?(:collection)
47
- # Try 3: Assume the input name will be the same as the column type (e.g. string_input).
48
- return column.type
49
- else
50
- return :select if options.key?(:collection)
51
- return :password if method.to_s =~ /password/
52
- return :string
53
- end
54
- end
55
-
56
- def column_for(method) #:nodoc:
57
- @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute)
58
- end
59
-
60
- # Takes the `:as` option and attempts to return the corresponding input class. In the case of
61
- # `:as => :string` it will first attempt to find a top level `StringInput` class (to allow the
62
- # application to subclass and modify to suit), falling back to `Formtastic::Inputs::StringInput`.
63
- #
64
- # This also means that the application can define it's own custom inputs in the top level
65
- # namespace (eg `DatepickerInput`).
66
- #
67
- # @param [Symbol] as A symbol representing the type of input to render
68
- # @raise [Formtastic::UnknownInputError] An appropriate input class could not be found
69
- # @return [Class] An input class constant
70
- #
71
- # @example Normal use
72
- # input_class(:string) #=> Formtastic::Inputs::StringInput
73
- # input_class(:date) #=> Formtastic::Inputs::DateInput
74
- #
75
- # @example When a top-level class is found
76
- # input_class(:string) #=> StringInput
77
- # input_class(:awesome) #=> AwesomeInput
78
- def input_class(as)
79
- @input_classes_cache ||= {}
80
- @input_classes_cache[as] ||= begin
81
- begin
82
- begin
83
- custom_input_class_name(as).constantize
84
- rescue NameError
85
- standard_input_class_name(as).constantize
86
- end
87
- rescue NameError
88
- raise Formtastic::UnknownInputError
89
- end
90
- end
91
- end
92
-
93
- # :as => :string # => StringInput
94
- def custom_input_class_name(as)
95
- "#{as.to_s.camelize}Input"
96
- end
97
-
98
- # :as => :string # => Formtastic::Inputs::StringInput
99
- def standard_input_class_name(as)
100
- "Formtastic::Inputs::#{as.to_s.camelize}Input"
101
- end
102
-
103
- end
104
- end
105
- end