model_manage 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/.DS_Store ADDED
Binary file
data/lib/model_manage.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'ostruct'
2
2
  require 'model_manage/mongoid'
3
3
  require 'model_manage/active_record'
4
- require 'model_manage/mongoid_as_active_record'
5
4
 
6
5
  require 'model_manage/rails'
7
6
  require 'model_manage/bitfield'
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,105 @@
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
@@ -22,6 +22,7 @@ module Mongoid
22
22
  end
23
23
  end
24
24
 
25
+
25
26
  module ModelManage
26
27
  module Mongoid
27
28
  def self.included(base)
@@ -50,12 +51,96 @@ module ModelManage
50
51
  relation_form_set name, options
51
52
  end
52
53
 
53
- # for sinple form
54
- def column_for_attribute(attribute_name)
55
- self.class.forms[attribute_name.to_s]
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
56
102
  end
103
+ end
57
104
  end
58
105
  end
59
106
  end
60
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
+ end
143
+ end
61
144
  end
145
+
146
+ end
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.3"
8
+ s.version = "0.1.4"
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 = "2011-12-03"
12
+ s.date = "2012-01-01"
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 = [
@@ -23,12 +23,14 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "VERSION",
26
+ "lib/.DS_Store",
26
27
  "lib/model_manage.rb",
27
28
  "lib/model_manage/active_record.rb",
28
29
  "lib/model_manage/base.rb",
29
30
  "lib/model_manage/bitfield.rb",
31
+ "lib/model_manage/formtastic/array_input.rb",
32
+ "lib/model_manage/formtastic/input_helper.rb",
30
33
  "lib/model_manage/mongoid.rb",
31
- "lib/model_manage/mongoid_as_active_record.rb",
32
34
  "lib/model_manage/rails.rb",
33
35
  "model_manage.gemspec",
34
36
  "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.3
4
+ version: 0.1.4
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: 2011-12-03 00:00:00.000000000 Z
12
+ date: 2012-01-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: simple_form
16
- requirement: &70108178669500 !ruby/object:Gem::Requirement
16
+ requirement: &70120811590500 !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: *70108178669500
24
+ version_requirements: *70120811590500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongoid
27
- requirement: &70108178668800 !ruby/object:Gem::Requirement
27
+ requirement: &70120811589420 !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: *70108178668800
35
+ version_requirements: *70120811589420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &70108178668240 !ruby/object:Gem::Requirement
38
+ requirement: &70120811588720 !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: *70108178668240
46
+ version_requirements: *70120811588720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &70108178667300 !ruby/object:Gem::Requirement
49
+ requirement: &70120811588040 !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: *70108178667300
57
+ version_requirements: *70120811588040
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rcov
60
- requirement: &70108178682720 !ruby/object:Gem::Requirement
60
+ requirement: &70120811587180 !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: *70108178682720
68
+ version_requirements: *70120811587180
69
69
  description: モデル、フィールドといった情報へ簡単アクセスすることを目的に開発しています。
70
70
  email: 7korobi@gmail.com
71
71
  executables: []
@@ -80,12 +80,14 @@ files:
80
80
  - README.rdoc
81
81
  - Rakefile
82
82
  - VERSION
83
+ - lib/.DS_Store
83
84
  - lib/model_manage.rb
84
85
  - lib/model_manage/active_record.rb
85
86
  - lib/model_manage/base.rb
86
87
  - lib/model_manage/bitfield.rb
88
+ - lib/model_manage/formtastic/array_input.rb
89
+ - lib/model_manage/formtastic/input_helper.rb
87
90
  - lib/model_manage/mongoid.rb
88
- - lib/model_manage/mongoid_as_active_record.rb
89
91
  - lib/model_manage/rails.rb
90
92
  - model_manage.gemspec
91
93
  - test/helper.rb
@@ -105,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
107
  version: '0'
106
108
  segments:
107
109
  - 0
108
- hash: 4055629282682359364
110
+ hash: -3390306210837111882
109
111
  required_rubygems_version: !ruby/object:Gem::Requirement
110
112
  none: false
111
113
  requirements:
@@ -1,73 +0,0 @@
1
- if defined?(Mongoid::Document) && defined?(ActiveRecord::Base)
2
-
3
- module ModelManage
4
- module Mongoid
5
- def self.included(base)
6
- def base.inheritance_column
7
- '_type'
8
- end
9
-
10
- def base.columns
11
- forms.values
12
- end
13
- def base.columns_hash
14
- forms
15
- end
16
- def base.descends_from_active_record?
17
- Rails.child_models.member? self
18
- end
19
- def base.reflect_on_all_associations(macro = nil)
20
- association_reflections = relations.values
21
- macro ? relations.select { |reflection| reflection.macro == macro } : association_reflections
22
- end
23
-
24
- def base.base_class
25
- class_of_active_record_descendant(self)
26
- end
27
- def base.class_of_active_record_descendant(klass)
28
- if not Rails.models.member? klass.superclass
29
- klass
30
- else
31
- class_of_active_record_descendant(klass.superclass)
32
- end
33
- end
34
- end
35
- end
36
-
37
- module Metadata
38
- def options
39
- form.data.merge(self)
40
- end
41
- def active_record
42
- form.owner
43
- end
44
- def check_validity!
45
- nil
46
- end
47
- def belongs_to?
48
- [:referenced_in, :embedded_in].member? macro
49
- end
50
- def collection?
51
- not belongs_to?
52
- end
53
- def through_reflection
54
- active_record.relations[ form.options[:through].to_s ]
55
- end
56
- end
57
- end
58
-
59
- class Mongoid::Relations::Metadata
60
- include ModelManage::Metadata
61
- end
62
-
63
- module ActiveRecord
64
- class Base
65
- def self.descendants
66
- Rails.models
67
- end
68
- end
69
- end
70
-
71
- end
72
-
73
-