support_segment 0.0.4

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 185c390313e63928b711e35f0ba86c17229e3426
4
+ data.tar.gz: fd0893634af5e8ac8e703a9b2d05837dfc0a819b
5
+ SHA512:
6
+ metadata.gz: 473e0abaf78f4371fda6604142ffb17f8fb14eec2e42cc899d0acf5e710ea98c34da89df215386e096e9e0116a31dfadbdb18c5261b20e979c0bee213a181795
7
+ data.tar.gz: 3d42775d2518068244a9bf19481ecf0aad1d72ea49419617006b415514c19056aeb98f6e3ebd06c6b777ae03093654e3bc1929ef6dba433e4f978f3c551ee7f8
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,5 @@
1
+ = SupportSegment
2
+
3
+ Loose collection of extra functionality for rails apps
4
+
5
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SupportSegment'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
@@ -0,0 +1,4 @@
1
+ module SupportSegment
2
+ end
3
+
4
+ require 'support_segment/railtie' if defined?(Rails)
@@ -0,0 +1,96 @@
1
+ module ActiveRecord
2
+ class Relation
3
+
4
+ alias_method :relation_delete_all, :delete_all
5
+ def delete_all(conditions = nil)
6
+ return relation_delete_all(conditions) if !@klass.acts_as_citier?
7
+
8
+ return relation_delete_all(conditions) if conditions
9
+
10
+ deleted = true
11
+ ids = nil
12
+ c = @klass
13
+
14
+ bind_values.each do |bind_value|
15
+ if bind_value[0].name == "id"
16
+ ids = bind_value[1]
17
+ break
18
+ end
19
+ end
20
+ ids ||= where_values_hash["id"] || where_values_hash[:id]
21
+ where_hash = ids ? { :id => ids } : nil
22
+
23
+ deleted &= c.base_class.where(where_hash).relation_delete_all
24
+ while c.superclass != ActiveRecord::Base
25
+ if c.const_defined?(:Writable)
26
+ citier_debug("Deleting back up hierarchy #{c}")
27
+ deleted &= c::Writable.where(where_hash).delete_all
28
+ end
29
+ c = c.superclass
30
+ end
31
+
32
+ deleted
33
+ end
34
+
35
+ alias_method :relation_to_a, :to_a
36
+ def to_a
37
+ return relation_to_a if !@klass.acts_as_citier?
38
+
39
+ records = relation_to_a
40
+
41
+ c = @klass
42
+
43
+ if records.all? { |record| record.class == c }
44
+ return records
45
+ end
46
+
47
+ full_records = []
48
+ ids_wanted = {}
49
+
50
+ # Map all the ids wanted per type
51
+ records.each do |record|
52
+ if record.class == c # We don't need to find the record again if this is already the correct one
53
+ full_records << record
54
+ next
55
+ end
56
+
57
+ ids_wanted[record.class] ||= []
58
+ ids_wanted[record.class] << record.id
59
+ end
60
+
61
+ # Find all wanted records
62
+ ids_wanted.each do |type_class, ids|
63
+ full_records.push(*type_class.find(ids))
64
+ end
65
+
66
+ # Make a new array with the found records at the right places
67
+ records.each do |record|
68
+ full_record = full_records.find { |full_record| full_record.id == record.id }
69
+ record.force_attributes(full_record.instance_variable_get(:@attributes), :merge => true, :clear_caches => false)
70
+ end
71
+
72
+ return records
73
+ end
74
+
75
+ alias_method :relation_apply_finder_options, :apply_finder_options
76
+ def apply_finder_options(options)
77
+ return relation_apply_finder_options(options) if !@klass.acts_as_citier?
78
+
79
+ relation = self
80
+
81
+ # With option :no_children set to true, only records of type self will be returned.
82
+ # So Root.all(:no_children => true) won't return Child records.
83
+ no_children = options.delete(:no_children)
84
+ if no_children
85
+ relation = clone
86
+
87
+ c = @klass
88
+
89
+ self_type = c.superclass == ActiveRecord::Base ? nil : c.name
90
+ relation = relation.where(:type => self_type)
91
+ end
92
+
93
+ relation.relation_apply_finder_options(options)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,23 @@
1
+ module SupportSegment
2
+ class FormModel
3
+
4
+ class NotImplemented < Exception; end
5
+ class NotValid < Exception; end
6
+
7
+ extend ActiveModel::Naming
8
+ include ActiveModel::Conversion
9
+ include ActiveModel::Validations
10
+
11
+ def initialize(attributes = {})
12
+ attributes.each do |name, value|
13
+ send("#{name}=", value)
14
+ end
15
+ end
16
+
17
+ # Forms are never themselves persisted
18
+ def persisted?
19
+ false
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module SupportSegment
2
+ module MethodDecorator
3
+
4
+ def before(method, &block)
5
+ decorate(method, :before, block)
6
+ end
7
+
8
+ def after(method, &block)
9
+ decorate(method, :after, block)
10
+ end
11
+
12
+ def around(method, &block)
13
+ decorate(method, :around, block)
14
+ end
15
+
16
+ private
17
+ def decorate(method, callback, block)
18
+ old_method = instance_method(method)
19
+
20
+ define_method(method) do |*args|
21
+ if [:before, :around].include? callback
22
+ self.instance_eval &block
23
+ end
24
+
25
+ old_method.bind(self).call(*args)
26
+
27
+ if [:after, :around].include? callback
28
+ self.instance_eval &block
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ module SupportSegment
2
+ module MobileDetect
3
+ class Constraint
4
+
5
+ def matches?(request)
6
+ return false if request.cookies[:full_site_form_mobile] || request.host.match(/^m./)
7
+ request.user_agent.to_s.match(/Mobile/)
8
+ end
9
+
10
+ end
11
+
12
+
13
+ # the following sucks and it makes sense to be more in control of what routes you want to expose for the mobile experience
14
+ class Middleware
15
+
16
+ def initialize(app)
17
+ @app = app
18
+ end
19
+
20
+ def call(env)
21
+ begin
22
+ request = Rack::Request.new(env)
23
+ if request.host.match(/^m./)
24
+ request.params[:format] = :mobile
25
+ request.env["action_dispatch.request.formats"] = [Mime::Type.lookup_by_extension(:mobile)]
26
+ end
27
+
28
+ return @app.call(env)
29
+ rescue => exception
30
+ Rails.logger.fatal(
31
+ "\n#{exception.class} (#{exception.message}):\n " +
32
+ exception.backtrace.join("\n") + "\n\n"
33
+ )
34
+ raise exception
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'support_segment/mobile_detect'
2
+
3
+ module SupportSegment
4
+ class Railtie < Rails::Railtie
5
+ initializer "support_segment.configure_rails_initialization" do |app|
6
+ Mime::Type.register_alias "text/html", :mobile
7
+ app.middleware.use MobileDetect::Middleware
8
+ end
9
+
10
+ initializer 'support_segment.validators', :before => :set_autoload_paths do |app|
11
+ app.config.autoload_paths << File.expand_path("../../validators", __FILE__)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,114 @@
1
+ #-------------------------------------------------------------------------------
2
+ # From: https://gist.github.com/1139595
3
+ # This module is meant to be included in the Base class of an STI model
4
+ # hierarchy.
5
+ #
6
+ # Heavily inspired by a couple key blog posts
7
+ # http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html
8
+ # http://coderrr.wordpress.com/2008/04/22/building-the-right-class-with-sti-in-rails/#comment-1826
9
+ #
10
+ # Thanks to Alex and coderrr!
11
+ #-------------------------------------------------------------------------------
12
+ module SupportSegment
13
+ module StiHelpers
14
+
15
+ def self.included(base)
16
+ base.send :include, InstanceMethods
17
+ base.extend ClassMethods
18
+ end
19
+
20
+ module ClassMethods
21
+
22
+
23
+ def select_options
24
+ descendants.map{ |c| c.to_s }.sort
25
+ end
26
+
27
+ def sti_helpers_base
28
+ true
29
+ end
30
+
31
+ def sti_association_extensions
32
+ @sti_association_extensions ||= Module.new
33
+ end
34
+
35
+ def inherited(child)
36
+ super
37
+ base = sti_base_class
38
+
39
+
40
+ child.instance_eval do
41
+ def self.sti_helpers_base
42
+ false
43
+ end
44
+
45
+ def model_name
46
+ sti_base_class.model_name
47
+ end
48
+ end
49
+
50
+ method_name = :"#{child.name.to_s.demodulize.underscore.pluralize}"
51
+
52
+ base.define_singleton_method method_name do
53
+ where(inheritance_column.to_sym => child.name)
54
+ end
55
+
56
+ sti_association_extensions.send :define_method, method_name do
57
+ relation = where(inheritance_column.to_sym => child.name)
58
+ relation.define_singleton_method :build do |*args, &block|
59
+ result = super(*args, &block)
60
+ proxy_association.add_to_target(result)
61
+ end
62
+
63
+ relation
64
+ end
65
+
66
+ end
67
+
68
+ def sti_base_class
69
+ # TODO: use the included hook to set the sti_base_class class?
70
+ if self.sti_helpers_base
71
+ return self
72
+ end
73
+ return superclass.sti_base_class
74
+ end
75
+
76
+ def implied_inheritance_class(*inheritance_type_sources)
77
+ if scope_values = self.current_scope.try(:where_values_hash)
78
+ inheritance_type_sources << scope_values
79
+ end
80
+
81
+ valid_sources = inheritance_type_sources.select do |source|
82
+ source.is_a? Hash
83
+ end
84
+
85
+ inheritance_type = valid_sources.inject(nil) do |type, values|
86
+ type ? type : values.with_indifferent_access[inheritance_column]
87
+ end
88
+
89
+ inheritance_type
90
+ end
91
+
92
+ # !! with the first conditional clause type logic will only apply to base class
93
+ # this MAY not be what you'd want, in which case ommit.
94
+ def new(*a, &b)
95
+ if (self == sti_base_class) \
96
+ and (subclass_name = implied_inheritance_class(a.first)) \
97
+ and (subclass = subclass_name.safe_constantize) != self
98
+ raise "wtF hax!!" unless subclass < self # klass should be a descendant of us
99
+ return subclass.new(*a, &b)
100
+ end
101
+ super(*a, &b)
102
+ end
103
+
104
+ end
105
+
106
+ module InstanceMethods
107
+ def sti_base_class
108
+ self.class.sti_base_class
109
+ end
110
+ end
111
+
112
+ end
113
+
114
+ end
@@ -0,0 +1,3 @@
1
+ module SupportSegment
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :support_segment do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,23 @@
1
+ require 'active_model/validations'
2
+
3
+ class ReceiverValidator < ActiveModel::EachValidator
4
+
5
+ def validate_each(record, attribute, value)
6
+ if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?(record.validation_context) }.any?
7
+ receiver = record.public_method(attribute).call
8
+ if options[:map_attributes]
9
+ options[:map_attributes].each do |receiver_attribute, command_attribute|
10
+ if errors = receiver.errors.delete(receiver_attribute)
11
+ errors.each do |error|
12
+ record.errors.add(command_attribute, error)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ unless receiver.errors.empty?
18
+ record.errors.add(attribute, :invalid, options.merge(value: value))
19
+ end
20
+ end
21
+ end
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: support_segment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Leandro Pedroni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: capybara
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: poltergeist
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rb-fsevent
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Support modules for rails applications.
140
+ email:
141
+ - ilpoldo@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - MIT-LICENSE
147
+ - README.rdoc
148
+ - Rakefile
149
+ - lib/support_segment.rb
150
+ - lib/support_segment/cti_helpers.rb
151
+ - lib/support_segment/form_model.rb
152
+ - lib/support_segment/method_decorator.rb
153
+ - lib/support_segment/mobile_detect.rb
154
+ - lib/support_segment/railtie.rb
155
+ - lib/support_segment/sti_helpers.rb
156
+ - lib/support_segment/version.rb
157
+ - lib/tasks/support_segment_tasks.rake
158
+ - lib/validators/receiver_validator.rb
159
+ homepage: https://github.com/ilpoldo/support_segment
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.2.2
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: Support modules for rails applications.
183
+ test_files: []