model_subsets 0.0.4 → 0.0.5

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.
@@ -1,3 +1,3 @@
1
1
  module ModelSubsets
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/model_subsets.rb CHANGED
@@ -245,7 +245,7 @@ module ModelSubsets
245
245
  #
246
246
  # @since 0.0.2
247
247
  def subsets_scope name
248
- subsets_scopes[name]
248
+ subsets_scopes[name] || []
249
249
  end
250
250
 
251
251
  # Return subsets scopes list
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_subsets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-23 00:00:00.000000000 Z
12
+ date: 2012-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -41,7 +41,6 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - lib/model_subsets.rb
44
- - lib/model_subsets/class_methods.rb
45
44
  - lib/model_subsets/version.rb
46
45
  - model_subsets.gemspec
47
46
  homepage: https://github.com/porecreat/model_subets
@@ -1,130 +0,0 @@
1
- module ModelSubsets
2
- module ClassMethods
3
-
4
- # Creates a fieldset
5
- def fieldset *args
6
- fieldset = args.shift
7
- raise "fieldset id must be a Symbol (#{fieldset.class} given)" unless fieldset.is_a? Symbol
8
- raise "fieldset fields must be an Array (#{args.class} given)" unless args.is_a? Array
9
- args.each do |field|
10
- raise "field name must be an Symbol (#{field.class} given)" unless field.is_a? Symbol
11
- end
12
- @fieldsets ||= {}
13
- @fieldsets[fieldset] = args
14
- end
15
-
16
- # Creates a subset
17
- def subset subset, options = {}
18
- raise "subset id must be a Symbol (#{subset.class} given)" unless subset.is_a? Symbol
19
- raise "subset options must be a Hash (#{options.class} given)" unless options.is_a? Hash
20
- @subsets_config ||= {}
21
- @subsets_config[subset] = options
22
- end
23
-
24
- # Provides subset fieldset list
25
- def subset_fieldset subset
26
- return unless subsets.has_key?(subset) && subsets[subset].has_key?(:fieldsets)
27
- subset_fields = []
28
- subsets[subset][:fieldsets].each do |fieldset|
29
- fieldset = @fieldsets[fieldset].is_a?(Array) ? @fieldsets[fieldset] : [@fieldsets[fieldset]]
30
- subset_fields |= fieldset
31
- end
32
- subset_fields.uniq
33
- end
34
-
35
- # Provides subsets with builded extends & fieldsets
36
- def subsets options = {}
37
- @subsets_config ||= {}
38
- raise "subsets config must ba a Hash (#{@subsets_config.class} given)" unless @subsets_config.is_a? Hash
39
-
40
- # Cache builded subsets
41
- return @subsets unless @subsets.blank? || options[:purge]
42
-
43
- @subsets = {}
44
- @scopes = {}
45
-
46
- @subsets_config.each do |subset, options|
47
- # Can't define same subset twice
48
- raise "subset '#{subset} is already defined" if @subsets.has_key? subset
49
-
50
- # Subset is an extension
51
- if options[:extends]
52
-
53
- # Force extends option to Array
54
- options[:extends] = [options[:extends]] unless options[:extends].is_a? Array
55
- options[:extends].each do |source_subset|
56
- next unless @subsets.has_key? source_subset
57
- source_options = @subsets[source_subset].clone
58
- source_options.delete :abstract
59
- options = source_options.merge options
60
- end
61
-
62
- # Handle additional fieldsets list
63
- if options[:with]
64
- options[:with] = [options[:with]] unless options[:with].is_a? Array
65
- options[:fieldsets] |= options[:with]
66
- end
67
-
68
- else
69
- # Include all fieldsets by default
70
- options[:fieldsets] = @fieldsets.keys unless options[:fieldsets].is_a? Array
71
- end
72
-
73
- # Handle inclusion list
74
- if options[:only]
75
- options[:only] = [options[:only]] unless options[:only].is_a? Array
76
- options[:fieldsets] &= options[:only]
77
- end
78
-
79
- # Handle exclusion list
80
- if options[:except]
81
- options[:except] = [options[:except]] unless options[:except].is_a? Array
82
- options[:fieldsets] -= options[:except]
83
- end
84
-
85
- # Handle scopes
86
- options[:scopes] = [options[:scopes]] unless options[:scopes].is_a? Array
87
- options[:scopes].each do |scope|
88
- @scopes[scope] ||= []
89
- @scopes[scope] << subset
90
- end
91
-
92
- # Cleanup
93
- options[:fieldsets] = options[:fieldsets].uniq & @fieldsets.keys
94
- remove_options = [:extends, :with, :only, :except]
95
- options = options.clone
96
- options.delete_if{ |key, value| remove_options.include?(key) }
97
- @subsets[subset] = options
98
- end
99
-
100
- @subsets = @subsets.reject{|key, value| value[:abstract] == true}
101
- end
102
-
103
- # Provides subsets groups list formatted for use with grouped collection select
104
- def subsets_groups
105
- groups = {}
106
- i18n_group = {}
107
-
108
- subsets.each do |subset, options|
109
- raise "subset id must be a Symbol (#{subset.class} given)" unless subset.is_a? Symbol
110
-
111
- # Set default group
112
- options[:group] = :default unless options[:group]
113
-
114
- raise "group id must be a Symbol (#{options[:group].class} given)" unless options[:group].is_a? Symbol
115
-
116
- i18n_subset = self.human_attribute_name("subsets.#{subset}")
117
- i18n_group[options[:group]] ||= self.human_attribute_name("subsets.#{options[:group]}")
118
- groups[i18n_group[options[:group]]] ||= [options[:group], {}]
119
- groups[i18n_group[options[:group]]].last[i18n_subset] = subset
120
- end
121
-
122
- # Rearrange groups
123
- groups = groups.sort
124
- groups.map do |group|
125
- [group.last.first, group.first, group.last.last.sort]
126
- end
127
- end
128
-
129
- end
130
- end