inputex 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/inputex.rb +180 -0
  3. metadata +87 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d4d0830102eb122a507a00272dd14f82deaad35b
4
+ data.tar.gz: f067052af8178292c6f65bcee821e60dbe10e29f
5
+ SHA512:
6
+ metadata.gz: ecbbfdc5e1dce175755437a8a5675c416c74152068ff321cbfbc95c7d51555b5c583c09addb141503eea2a2d43027325a5da63a22ff1434f431080ff424228d9
7
+ data.tar.gz: 40e1fdedc3dd2f951d4622664220b4c20bf3637b921043c3ab60c4ff776695be6ebca687eef0ddee8eb2c0363302a3dff4442191bb1348b4e802325d30361b5c
@@ -0,0 +1,180 @@
1
+ # Inputex
2
+ # =======
3
+ #
4
+ # Helpers to generate inputEx forms from ActiveRecord models.
5
+ #
6
+ #
7
+ # * fields named "id" are hidden
8
+ # * "created_at", "updated_at" are uneditable
9
+ # * "email", "url", "password", "colorref", "ip" are rendered using the corresponding inputEx field
10
+ #
11
+ # * columns finishing by "_id" are rendered with their own type, corresponding to the REFLECTION CLASS
12
+ # Ex:
13
+ # A class with a "owner_id" column, and this in the model:
14
+ # belongs_to :owner, :class_name => "Group"
15
+ #
16
+ # => the generated type will be "group_id" (and not "owner_id")
17
+ #
18
+ # * renders datetime, boolean, text, integers, ...
19
+ #
20
+ # * renders select according to the ActiveModel validations
21
+ #
22
+ # validates_inclusion_of :format, :in => ["json", "xml", "urlencoded"]
23
+ # OR
24
+ # validates :format, :inclusion => { :in => ["json", "xml", "urlencoded"] }
25
+ #
26
+ #
27
+ # Usage
28
+ # =====
29
+ #
30
+ # * I18n MUST be configured.
31
+ # The plugin will look for the key "activerecord.attributes.#{self.name.downcase}.#{column.name}"
32
+ #
33
+ # # Get all fields (The order is generally wrong)
34
+ # MyModel.inputex_fields_definition
35
+ #
36
+ # # Get the fields "id" and "method" (in the same order)
37
+ # MyModel.inputex_fields_definition(["id","method"])
38
+ #
39
+ # # Get a definition given a column
40
+ # MyModel.inputex_field_definition(column, force_uneditable = false)
41
+ #
42
+ # # Get a definition given a column name
43
+ # MyModel.inputex_field_byname(columnName, force_uneditable = false)
44
+ #
45
+ #
46
+ # Copyright (c) 2010-2013 ClicRDV
47
+
48
+ require 'active_support'
49
+
50
+ module Inputex
51
+ module ModelExtensions
52
+
53
+ class FakeColumn
54
+ attr_accessor :name
55
+ attr_accessor :sql_type
56
+ attr_accessor :default
57
+ end
58
+
59
+ extend ::ActiveSupport::Concern
60
+
61
+ # "extend ClassMethods" is automatically executed at module's inclusion (thanks to ActiveSupport::Concern)
62
+ module ClassMethods
63
+
64
+ # Return a list of inputEx definitions for the given column names
65
+ def inputex_fields_definition(columnNames = self.columns_hash.keys)
66
+ columnNames.map { |c|
67
+ inputex_field_byname(c)
68
+ }
69
+ end
70
+
71
+ # Return the inputEx definition from a column name
72
+ def inputex_field_byname(columnName, force_uneditable = false)
73
+ column = self.columns_hash[columnName]
74
+
75
+ # Support for attribute accessors
76
+ if column.nil?
77
+ column = FakeColumn.new
78
+ column.name = columnName
79
+ end
80
+
81
+ inputex_field_definition(column, force_uneditable)
82
+ end
83
+
84
+ # Generate an inputEx definition from the column object
85
+ def inputex_field_definition(column, force_uneditable = false)
86
+
87
+ field = {
88
+ :type => "string",
89
+ :label => (I18n.t "activerecord.attributes.#{self.name.underscore}.#{column.name}").capitalize,
90
+ :name => column.name,
91
+ :showMsg => true
92
+ }
93
+
94
+ # Field type
95
+ # TODO: utiliser les types rails
96
+ if force_uneditable
97
+ field[:type] = "uneditable"
98
+
99
+ elsif column.name == "created_at" || column.name == "updated_at"
100
+ field[:type] = "uneditable"
101
+
102
+ elsif column.name == "id"
103
+ field[:type] = "hidden"
104
+
105
+ elsif column.name == "email"
106
+ field[:type] = "email"
107
+
108
+ elsif column.name == "password" || column.name == "hashed_password"
109
+ field[:type] = "password"
110
+
111
+ elsif column.name == "colorref"
112
+ field[:type] = "color"
113
+
114
+ elsif column.name == "ip"
115
+ field[:type] = "IPv4"
116
+
117
+ elsif column.name == "url"
118
+ field[:type] = "url"
119
+
120
+ elsif column.name[-3..-1] == "_id" # Autocompleters for liaisons
121
+ # The name of the attribute might be different than the name of the class
122
+ # (ex: Leaf.parent_id refering to another Leaf, the inputEx type is still "leaf_id")
123
+ liaisonName = (column.name[0..-4]).to_sym
124
+ reflection = self.reflections[ liaisonName.to_sym ]
125
+ if reflection
126
+ field[:type] = reflection.klass.to_s.downcase+"_id"
127
+ else
128
+ field[:type] = column.name
129
+ end
130
+
131
+ elsif column.sql_type == "datetime"
132
+ field[:type] = "datetime"
133
+
134
+ elsif column.sql_type == "tinyint(1)"
135
+ field[:type] = "boolean"
136
+
137
+ elsif column.sql_type == "int(11)"
138
+ field[:type] = "integer"
139
+
140
+ elsif column.sql_type == "text"
141
+ field[:type] = "text"
142
+ field[:rows] = 6
143
+ field[:cols] = 70
144
+
145
+ else
146
+
147
+ # test presence of an InclusionValidator
148
+ inclusion_validators = self.validators_on(column.name).select { |validator|
149
+ validator.is_a?(ActiveModel::Validations::InclusionValidator)
150
+ }
151
+
152
+ if inclusion_validators.size > 0
153
+ field[:type] = "select"
154
+ field[:choices] = inclusion_validators[0].send(:delimiter) # use send cause delimiter is private
155
+ end
156
+
157
+ end
158
+
159
+ # Default value
160
+ if !column.default.nil?
161
+ field[:value] = column.default
162
+ end
163
+
164
+ # Required ?
165
+ #if !column.null
166
+ # field[:required] = true
167
+ #end
168
+
169
+ field
170
+
171
+ end
172
+
173
+ end
174
+
175
+ end
176
+ end
177
+
178
+ ActiveSupport.on_load(:active_record) do
179
+ include Inputex::ModelExtensions
180
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inputex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maxime Réty
8
+ - Eric Abouaf
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '='
33
+ - !ruby/object:Gem::Version
34
+ version: 3.2.16
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '='
40
+ - !ruby/object:Gem::Version
41
+ version: 3.2.16
42
+ - !ruby/object:Gem::Dependency
43
+ name: coveralls
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: ''
57
+ email: maxime.rety@clicrdv.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/inputex.rb
63
+ homepage: http://rubygems.org/gems/inputex
64
+ licenses:
65
+ - Apache License 2.0
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.1.11
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Interface the inputex UI library with ActiveModel
87
+ test_files: []