model_subsets 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/model_subsets.rb +149 -0
- data/lib/model_subsets/version.rb +3 -0
- data/model_subsets.gemspec +17 -0
- metadata +54 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Gauthier Delacroix
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ModelSubsets
|
2
|
+
|
3
|
+
Model Subsets provides subsets management for models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'model_subsets'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install model_subsets
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require "model_subsets/version"
|
2
|
+
|
3
|
+
module ModelSubsets
|
4
|
+
|
5
|
+
# Includes class methods
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ModelSubsets::ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
# Provides current subset fieldset list
|
11
|
+
def fieldset
|
12
|
+
self.class.subset_fieldset(self.subset)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Whether a field is included in subset fieldset
|
16
|
+
def has_field? field
|
17
|
+
fieldset.include? field
|
18
|
+
end
|
19
|
+
|
20
|
+
# Whether subset has a fieldset
|
21
|
+
def has_fieldset? fieldset
|
22
|
+
return unless self.class.subsets.has_key? self.subset
|
23
|
+
self.class.subsets[self.subset][:fieldsets].include? fieldset
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid_subset?
|
27
|
+
return true if self.class.subsets.keys.include? self.subset
|
28
|
+
errors.add(:subset, :invalid) if self.respond_to? errors
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
|
34
|
+
def fieldset *args
|
35
|
+
fieldset = args.shift
|
36
|
+
raise "fieldset id must be a Symbol (#{fieldset.class} given)" unless fieldset.is_a? Symbol
|
37
|
+
raise "fieldset fields must be an Array (#{args.class} given)" unless args.is_a? Array
|
38
|
+
args.each do |field|
|
39
|
+
raise "field name must be an Symbol (#{field.class} given)" unless field.is_a? Symbol
|
40
|
+
end
|
41
|
+
@fieldsets ||= {}
|
42
|
+
@fieldsets[fieldset] = args
|
43
|
+
end
|
44
|
+
|
45
|
+
# Provides subset fieldset list
|
46
|
+
def subset_fieldset subset
|
47
|
+
return unless subsets.has_key? subset
|
48
|
+
subset_fields = []
|
49
|
+
subsets[subset][:fieldsets].each do |fieldset|
|
50
|
+
fieldset = @fieldsets[fieldset].is_a?(Array) ? @fieldsets[fieldset] : [@fieldsets[fieldset]]
|
51
|
+
subset_fields |= fieldset
|
52
|
+
end
|
53
|
+
subset_fields.uniq
|
54
|
+
end
|
55
|
+
|
56
|
+
def subset subset, options = {}
|
57
|
+
raise "subset id must be a Symbol (#{subset.class} given)" unless subset.is_a? Symbol
|
58
|
+
raise "subset options must be a Hash (#{options.class} given)" unless options.is_a? Hash
|
59
|
+
@subsets_config ||= {}
|
60
|
+
@subsets_config[subset] = options
|
61
|
+
end
|
62
|
+
|
63
|
+
# Provides subsets with builded extends & fieldsets
|
64
|
+
def subsets options = {}
|
65
|
+
@subsets_config ||= {}
|
66
|
+
raise "subsets config must ba a Hash (#{@subsets_config.class} given)" unless @subsets_config.is_a? Hash
|
67
|
+
|
68
|
+
# Cache builded subsets
|
69
|
+
return @subsets unless @subsets.blank? || options[:purge]
|
70
|
+
|
71
|
+
@subsets = {}
|
72
|
+
|
73
|
+
@subsets_config.each do |subset, options|
|
74
|
+
# Can't define same subset twice
|
75
|
+
raise "subset '#{subset} is already defined" if @subsets.has_key? subset
|
76
|
+
|
77
|
+
# Subset is an extension
|
78
|
+
if options[:extends]
|
79
|
+
|
80
|
+
# Force extends option to Array
|
81
|
+
options[:extends] = [options[:extends]] unless options[:extends].is_a? Array
|
82
|
+
options[:extends].each do |source_subset|
|
83
|
+
next unless @subsets.has_key? source_subset
|
84
|
+
source_options = @subsets[source_subset].clone
|
85
|
+
source_options.delete :abstract
|
86
|
+
options = source_options.merge options
|
87
|
+
end
|
88
|
+
|
89
|
+
# Handle additional fieldsets list
|
90
|
+
if options[:with]
|
91
|
+
options[:with] = [options[:with]] unless options[:with].is_a? Array
|
92
|
+
options[:fieldsets] |= options[:with]
|
93
|
+
end
|
94
|
+
|
95
|
+
else
|
96
|
+
# Include all fieldsets by default
|
97
|
+
options[:fieldsets] = @fieldsets.keys
|
98
|
+
end
|
99
|
+
|
100
|
+
# Handle inclusion list
|
101
|
+
if options[:only]
|
102
|
+
options[:only] = [options[:only]] unless options[:only].is_a? Array
|
103
|
+
options[:fieldsets] &= options[:only]
|
104
|
+
end
|
105
|
+
|
106
|
+
# Handle exclusion list
|
107
|
+
if options[:except]
|
108
|
+
options[:except] = [options[:except]] unless options[:except].is_a? Array
|
109
|
+
options[:fieldsets] -= options[:except]
|
110
|
+
end
|
111
|
+
|
112
|
+
# Cleanup
|
113
|
+
options[:fieldsets] = options[:fieldsets].uniq & @fieldsets.keys
|
114
|
+
remove_options = [:extends, :with, :only, :except]
|
115
|
+
options = options.clone
|
116
|
+
options.delete_if{ |key, value| remove_options.include?(key) }
|
117
|
+
@subsets[subset] = options
|
118
|
+
end
|
119
|
+
|
120
|
+
@subsets = @subsets.reject{|key, value| value[:abstract] == true}
|
121
|
+
end
|
122
|
+
|
123
|
+
# Provides subsets groups list formatted for use with grouped collection select
|
124
|
+
def subsets_groups
|
125
|
+
groups = {}
|
126
|
+
i18n_group = {}
|
127
|
+
|
128
|
+
subsets.each do |subset, options|
|
129
|
+
raise "subset id must be a Symbol (#{subset.class} given)" unless subset.is_a? Symbol
|
130
|
+
|
131
|
+
# Set default group
|
132
|
+
options[:group] = :default unless options[:group]
|
133
|
+
|
134
|
+
raise "group id must be a Symbol (#{options[:group].class} given)" unless options[:group].is_a? Symbol
|
135
|
+
|
136
|
+
i18n_subset = self.human_attribute_name("subsets.#{subset}")
|
137
|
+
i18n_group[options[:group]] ||= self.human_attribute_name("subsets.#{options[:group]}")
|
138
|
+
groups[i18n_group[options[:group]]] ||= [options[:group], {}]
|
139
|
+
groups[i18n_group[options[:group]]].last[i18n_subset] = subset
|
140
|
+
end
|
141
|
+
|
142
|
+
# Rearrange groups
|
143
|
+
groups = groups.sort
|
144
|
+
groups.map do |group|
|
145
|
+
[group.last.first, group.first, group.last.last.sort]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/model_subsets/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Gauthier Delacroix"]
|
6
|
+
gem.email = ["gauthier.delacroix@gmail.com"]
|
7
|
+
gem.description = %q{ModelSubsets provides ability to split a single model into fields and properties subsets}
|
8
|
+
gem.summary = %q{Subsets management for Rails models}
|
9
|
+
gem.homepage = "https://github.com/porecreat/model_subets"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "model_subsets"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ModelSubsets::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model_subsets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gauthier Delacroix
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ModelSubsets provides ability to split a single model into fields and
|
15
|
+
properties subsets
|
16
|
+
email:
|
17
|
+
- gauthier.delacroix@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/model_subsets.rb
|
28
|
+
- lib/model_subsets/version.rb
|
29
|
+
- model_subsets.gemspec
|
30
|
+
homepage: https://github.com/porecreat/model_subets
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.23
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Subsets management for Rails models
|
54
|
+
test_files: []
|