siphon 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fda2150f53756f1efb9c4b7799ef8b455058da6
4
- data.tar.gz: bf50ec379b961877f30111e9a692f9acb3725fc4
3
+ metadata.gz: ff59f1ee65f4df1a1dcd5a81d58f720516661b04
4
+ data.tar.gz: b8de584c9cc38381f08ff3728010465aab98ad61
5
5
  SHA512:
6
- metadata.gz: cf69ad74e5e7e24674f8c79b085cd8d44099dbf22f4ad6d096f714d6295d7fa49a3b95f277d04c906b6a4bbf5bb7a62d846bae5ccb75b7ebca0c60449b38fcbc
7
- data.tar.gz: 67d175542ffade84c13ce2116730d96f7c2fd6841b32e35938d90953ea79069df248e2743268c2815afc859b0929317f4641c191d2072777d6a91c9b2f72cbc5
6
+ metadata.gz: 976bb2d950083963335c855b4b7848eb84689094245eafb76c23d0e61fc5ea201ed1ca0b5da71b66f04af1a792ec73938c23ca7c22eef02f707f056a0979338b
7
+ data.tar.gz: e4b25e4bbe4844db09024f2e61939583f89d6e1daee028162c24740a614972d255703891aacd43a0604f5f5688599362adcaa75e68eb5fe6b4fd93906ccd495d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.2.3
data/.travis.yml CHANGED
@@ -2,6 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - "1.9.3"
4
4
  - "2.0.0"
5
+ - "2.2.3"
5
6
  #- jruby-18mode # JRuby in 1.8 mode
6
7
  #- jruby-19mode # JRuby in 1.9 mode
7
8
  #- rbx
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## Siphon 0.2.0 (Dec 2015)
2
+
3
+ * The ModelSearch can now respond_to `siphon_attributes`
4
+ * Boilerplate provides all the stuff ModelSearch needs...
5
+ * ...hence the generated ModelSearch is now minimal
6
+
7
+ ## Siphone 0.1.3
8
+
9
+ * handle time & dates arguments for scope from issue #2
@@ -1,59 +1,34 @@
1
1
  # uncomment `q` lines for combining ransack with your scope search
2
2
  class <%= class_name %>Search
3
3
 
4
- include ActiveModel::Model
5
- include Virtus.model
4
+ include Siphon::Boilerplate
5
+ siphonize class_name
6
6
 
7
- TABLE = <%= class_name %>.table_name
7
+ #
8
+ # Scope attributes
9
+ #
10
+ # attribute :tree_id, Integer
11
+ # attribute :is_public, Boolean
8
12
 
9
- attribute :tree_id, Integer
10
- attribute :is_public, Boolean
13
+ #
14
+ # ransack attributes
15
+ #
16
+ # ransack :category_id_eq
17
+ # ransack :title_cont
11
18
 
12
19
 
13
- attr_reader :q # the nested ransack object
14
- attr_reader :order_by # handles your order clause
15
-
16
- def initialize(params = {})
17
- @params = params || {}
18
- super(params)
19
- self.q= @params[:q]
20
- self.order_by= @params[:order_by]
21
- end
22
-
23
- # Example of conditionally applying search terms.
24
- # def done
25
- # return false unless @params[:q]
26
- # @params[:q][:state_eq].blank? ? @done : ""
27
- # end
28
-
29
- def result
30
- <%= class_name %>.scoped.order(order_by).merge(q.result).merge(siphoned)
31
- end
32
20
 
33
21
  # Exmaple of Search Form handling order stuff (might not be the best place)
34
22
  def self.order_by
35
- [['newest',"#{TABLE}.created_at DESC"],
36
- ["oldest", "#{TABLE}.created_at"],
37
- ["category", "#{TABLE}.category_id, #{TABLE}.id"]
23
+ [['newest',"#{table_name}.created_at DESC"],
24
+ ["oldest", "#{table_name}.created_at"],
25
+ ["category", "#{table_name}.category_id, #{table_name}.id"]
38
26
  # ["popularity", "sales_num DESC"]]
39
27
  ]
40
28
  end
41
29
 
42
30
  # Example of default ordering
43
31
  def order_by=( val )
44
- @order_by = val.blank? ? "#{TABLE}.created_at DESC" : val
32
+ @order_by = val.blank? ? "#{table_name}.created_at DESC" : val
45
33
  end
46
-
47
-
48
- def q=(sub_form_hash = {})
49
- @q = <%= class_name %>.search( sub_form_hash )
50
- end
51
-
52
- private
53
-
54
- def siphoned
55
- Siphon::Base.new(<%= class_name %>.scoped).scope( self )
56
- end
57
-
58
-
59
34
  end
@@ -12,8 +12,8 @@ module Siphon
12
12
  def initialize(formobj)
13
13
  @formobj = formobj
14
14
 
15
- @scopes_hash = @formobj.attributes
16
- @argless = argless_list(formobj)
15
+ @scopes_hash = assign_scope_hashes @formobj
16
+ @argless = argless_list @formobj
17
17
  end
18
18
 
19
19
  def call
@@ -22,9 +22,9 @@ module Siphon
22
22
  end
23
23
 
24
24
  private
25
- # if scope is present in form but with no value (aka: an empty string)
26
- # or if present in formobj but not in form (aka : a nil value)
27
- # don't apply the scope && reject them from scopes_hash
25
+ # Do NOT APPLY SCOPE && reject them from scopes_hash
26
+ # 1. if scope is present in form but with no value (aka: an empty string)
27
+ # 2. if present in formobj but not in form (aka : a nil value)
28
28
  def filterout_empty_string_and_nil
29
29
  @scopes_hash.delete_if { |scope, arg| ["", nil].include? @formobj[scope] }
30
30
  end
@@ -45,5 +45,11 @@ module Siphon
45
45
  map(&:name)
46
46
  end
47
47
 
48
+ # if the form object has #siphon_attributes favor that one
49
+ # usefull to seperate ransack attributes from siphon ones
50
+ def assign_scope_hashes(formobj)
51
+ formobj.respond_to?(:siphon_attributes) ?
52
+ formobj.siphon_attributes : formobj.attributes
53
+ end
48
54
  end
49
55
  end
@@ -0,0 +1,84 @@
1
+ require "active_support/concern"
2
+
3
+ module Siphon
4
+ module Boilerplate
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include ActiveModel::Model
9
+ include Virtus.model
10
+ include InstanceMethods
11
+
12
+ cattr_accessor :model
13
+ cattr_accessor :model_name
14
+ cattr_accessor :table_name
15
+
16
+
17
+ attr_reader :params_ransack # the ransack object
18
+ attr_reader :params_siphon # the ransack object
19
+ attr_reader :order_by # handles your order clause
20
+
21
+ end
22
+
23
+ module InstanceMethods
24
+ def initialize( params = {}, relation = nil )
25
+ @relation||= relation
26
+ @params = params || {}
27
+
28
+ @params_ransack= @params.slice(*self.class.ransack_set)
29
+ @params_siphon= @params.except(*self.class.ransack_set)
30
+ self.order_by= @params["order_by"]
31
+
32
+ super @params_siphon
33
+ end
34
+
35
+ def table_name
36
+ self.class.model.table_name
37
+ end
38
+
39
+ def ransack
40
+ @relation.search( params_ransack )
41
+ end
42
+
43
+ def merge(relation)
44
+ @relation = @relation.merge(relation)
45
+ self
46
+ end
47
+
48
+ def siphoned
49
+ Siphon::Base.new(@relation).scope( self )
50
+ end
51
+
52
+ # memoized or it'll break after attributes reconciled
53
+ def result
54
+ @result ||= begin
55
+ relation = @relation.order(order_by).
56
+ merge(ransack.result).
57
+ merge(siphoned)
58
+ # reconcile all params for the search form (?)
59
+ self.attributes= attributes.merge(params_ransack)
60
+ relation
61
+ end
62
+ end
63
+ end
64
+
65
+ class_methods do
66
+
67
+ def siphonize(model, model_name: nil, table_name: nil)
68
+ name = model_name || "#{model}Search"
69
+ self.model_name= ActiveModel::Name.new(self, nil, name)
70
+ self.table_name= Asset.table_name
71
+ end
72
+
73
+ def ransack_set
74
+ @ransack_set.map(&:to_s)
75
+ end
76
+
77
+ def ransack( attr, type= nil )
78
+ @ransack_set ||= []
79
+ attribute attr
80
+ @ransack_set << attr
81
+ end
82
+ end
83
+ end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module Siphon
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/siphon.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require "bundler/setup"
2
2
 
3
+ require "active_support"
3
4
  require "active_support/core_ext"
4
5
 
5
6
  require "siphon/version"
6
7
  require "siphon/base"
7
8
  require "siphon/adapter"
8
9
  require "siphon/nil"
10
+ require "siphon/boilerplate"
9
11
 
10
12
  module Siphon
11
13
  # Your code goes here...
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: siphon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Sistovaris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-24 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: virtus
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pry
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: activemodel
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: Siphon enables you to easily apply/combine/exclude your ActiveRecord
@@ -116,11 +116,12 @@ executables: []
116
116
  extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
- - .gitignore
120
- - .pryrc
121
- - .rspec
122
- - .ruby-version
123
- - .travis.yml
119
+ - ".gitignore"
120
+ - ".pryrc"
121
+ - ".rspec"
122
+ - ".ruby-version"
123
+ - ".travis.yml"
124
+ - CHANGELOG.md
124
125
  - Gemfile
125
126
  - LICENSE.txt
126
127
  - README.md
@@ -131,6 +132,7 @@ files:
131
132
  - lib/siphon.rb
132
133
  - lib/siphon/adapter.rb
133
134
  - lib/siphon/base.rb
135
+ - lib/siphon/boilerplate.rb
134
136
  - lib/siphon/nil.rb
135
137
  - lib/siphon/version.rb
136
138
  - siphon.gemspec
@@ -152,17 +154,17 @@ require_paths:
152
154
  - lib
153
155
  required_ruby_version: !ruby/object:Gem::Requirement
154
156
  requirements:
155
- - - '>='
157
+ - - ">="
156
158
  - !ruby/object:Gem::Version
157
159
  version: '0'
158
160
  required_rubygems_version: !ruby/object:Gem::Requirement
159
161
  requirements:
160
- - - '>='
162
+ - - ">="
161
163
  - !ruby/object:Gem::Version
162
164
  version: '0'
163
165
  requirements: []
164
166
  rubyforge_project:
165
- rubygems_version: 2.2.0
167
+ rubygems_version: 2.4.5.1
166
168
  signing_key:
167
169
  specification_version: 4
168
170
  summary: Siphon enables you to easily apply/combine/exclude your ActiveRecord scopes
@@ -176,4 +178,3 @@ test_files:
176
178
  - spec/siphon/mapper_spec.rb
177
179
  - spec/siphon_spec.rb
178
180
  - spec/spec_helper.rb
179
- has_rdoc: