trailblazer-rails 0.1.1 → 0.1.2

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: c0d4830a30173aac89597b022e250099c73a4df7
4
- data.tar.gz: c2c5eaf0fec7595f0ca30a6f7748d86f851b04de
3
+ metadata.gz: aac136aa3a451f9bb921b30839339a4025ad1190
4
+ data.tar.gz: 0fc514ed8bd3d5db1348ffd1204cdffe6f069e62
5
5
  SHA512:
6
- metadata.gz: 0df8dae649a7f02607399e3f5bd3358edb8a4d7f2d7ad16ee3e6f84d938aac0a7662b993ff44bf81245833b2d8a1731b37d17bce50b8ea5ad27ee395cfcb10a2
7
- data.tar.gz: 4e802dc7d231ee6a010ef9f715b42213da99a97c43fc39f073240bbd921104631e9743213e51660f3c5847b705ce26956c9b8a8d93397d1ddeb9947079a10def
6
+ metadata.gz: 3e67247d81be9ab89701789a2329862bf1ad99c538045c7125b4d9dd6efd74ce7e442c64cef5c0fc48955891ce40372542d79801bf12dee80d132c18f92d54b3
7
+ data.tar.gz: a75536b6d32c0665115653417abad1ac3dc1683431396958fdcd288cf0e5ed1fd333e0f52c6a860607a2e0d1fa820ca9977768692df9990b9611d0f549c5679a
@@ -0,0 +1,3 @@
1
+ # 0.1.2
2
+
3
+ * First version running with Trailblazer 1.0.0.
data/Gemfile CHANGED
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in trailblazer-rails.gemspec
4
4
  gemspec
5
5
 
6
- gem "trailblazer", path: "../trailblazer"
6
+ # gem "trailblazer", path: "../trailblazer"
@@ -0,0 +1,16 @@
1
+ # Assigns an additional instance variable for +@model+ named after the model's table name (e.g. @comment).
2
+ module Trailblazer::Operation::Controller::ActiveRecord
3
+ private
4
+ def setup_operation_instance_variables!(operation, options)
5
+ super
6
+ instance_variable_set(:"@#{operation_model_name}", @model)
7
+ end
8
+
9
+ def operation_model_name
10
+ # set the right variable name if collection
11
+ if @operation.is_a?(Trailblazer::Operation::Collection)
12
+ return @model.model.table_name.split(".").last
13
+ end
14
+ @model.class.table_name.split(".").last.singularize
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module Trailblazer::Operation::Responder
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def model_name
8
+ model_class.model_name
9
+ end
10
+ end
11
+
12
+ extend Forwardable
13
+ def_delegators :@model, :to_param, :destroyed?, :persisted?
14
+
15
+ def errors
16
+ return [] if @valid
17
+ [1]
18
+ end
19
+
20
+ def to_model
21
+ @model
22
+ end
23
+ end
@@ -0,0 +1,112 @@
1
+ require 'sidekiq/worker'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+
4
+ #setup in initialize: when Op.run() with Worker, the policy will be run "delayed" and not with the actual permission set. this will result in many crashing sidekiq workers.
5
+
6
+ class Trailblazer::Operation
7
+ # only kicks in when Operation::run, #run will still do it real-time
8
+ # Works with Reform 2, only.
9
+ module Worker
10
+ def self.included(base)
11
+ base.send(:include, Sidekiq::Worker)
12
+ base.extend(ClassMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+ def run(params)
17
+ if background?
18
+ return perform_async(serializable(params))
19
+ end
20
+
21
+ super(params)
22
+ end
23
+
24
+ def new(*args)
25
+ return super if args.any?
26
+ # sidekiq behavior: (not a big fan of this)
27
+ self
28
+ end
29
+
30
+ def perform(params) # called by Sidekiq.
31
+ build_operation(params).perform
32
+ end
33
+
34
+ def jid=(jid)
35
+ puts "@@@@@ #{jid.inspect}"
36
+ end
37
+
38
+ private
39
+ def perform_async(*args)
40
+ client_push('class' => self, 'args' => args) # calls class.new.perform(params)
41
+ end
42
+
43
+ def background? # TODO: make configurable.
44
+ true
45
+ # if Rails.env == "production" or Rails.env == "staging"
46
+ end
47
+
48
+ def serializable(params)
49
+ params # this is where we convert file uloads into Trailblazer::UploadedFile, etc. soon.
50
+ end
51
+ end
52
+
53
+
54
+ def perform#(params)
55
+ # the serialized params hash from Sidekiq contains a Op::UploadedFile hash.
56
+
57
+ # the following code is basically what happens in a controller.
58
+ # this is a bug in Rails, it doesn't work without requiring as/hash/ina
59
+ # params = ActiveSupport::HashWithIndifferentAccess.new_from_hash_copying_default(params) # TODO: this might make it ultra-slow as Reform converts it back to strings.
60
+ params = @params.with_indifferent_access
61
+ @params = deserializable(params)
62
+ run
63
+ end
64
+
65
+ private
66
+ def deserializable(params)
67
+ params # this is where we convert file uloads into Trailblazer::UploadedFile, etc. soon.
68
+ end
69
+
70
+
71
+ # Overrides ::serializable and #deserializable and handles file properties from the Contract schema.
72
+ module FileMarshaller
73
+ # NOTE: this is WIP and the implementation will be more understandable and performant soon.
74
+ def self.included(base)
75
+ base.extend ClassMethods
76
+ end
77
+
78
+
79
+ private
80
+ module ClassMethods
81
+ def file_marshaller_representer
82
+ @file_marshaller_representer ||= contract_class.schema(include: [Representable::Hash]).apply do |dfn|
83
+ dfn.merge!(
84
+ getter: lambda { |*| self[dfn.name.to_sym] },
85
+ setter: lambda { |fragment, *| self[dfn.name.to_s] = fragment }
86
+ ) # FIXME: allow both sym and str.
87
+
88
+ dfn.merge!(class: Hash) and next if dfn[:form] or dfn[:twin] # nested properties need a class for deserialization.
89
+ next unless dfn[:file]
90
+
91
+ # TODO: where do we set /tmp/uploads?
92
+ dfn.merge!(
93
+ serialize: lambda { |file, *| Trailblazer::Operation::UploadedFile.new(file, tmp_dir: "/tmp/uploads").to_hash },
94
+ deserialize: lambda { |object, hash, *| Trailblazer::Operation::UploadedFile.from_hash(hash) },
95
+ class: Hash
96
+ )
97
+ end
98
+ end
99
+
100
+ def serializable(params)
101
+ file_marshaller_representer.new(params).to_hash
102
+ end
103
+ end
104
+
105
+ # todo: do with_indifferent_access in #deserialize and call super here.
106
+ def deserializable(hash)
107
+ # self.class.file_marshaller_representer.new({}).extend(Representable::Debug).from_hash(hash)
108
+ self.class.file_marshaller_representer.new({}.with_indifferent_access).from_hash(hash)
109
+ end
110
+ end
111
+ end
112
+ end
@@ -8,9 +8,21 @@ end
8
8
 
9
9
  require "trailblazer/rails/railtie"
10
10
 
11
+ require "trailblazer/operation"
12
+ # TODO: remove that once i18n, validations etc in Reform/AM are sorted.
13
+ Trailblazer::Operation.contract_class.class_eval do
14
+ def self.name
15
+ # for whatever reason, validations climb up the inheritance tree and require _every_ class to have a name (4.1).
16
+ "Reform::Form"
17
+ end
18
+ end
19
+
11
20
  # Automatically set model_name on operation's contract when `Op::Model` is included.
12
21
  require "trailblazer/operation/model"
13
22
  require "trailblazer/operation/model/active_model"
14
23
  Trailblazer::Operation::Model::DSL.module_eval do
15
24
  include Trailblazer::Operation::Model::ActiveModel # ::contract.
16
- end
25
+ end
26
+
27
+ require "trailblazer/autoloading"
28
+ require "trailblazer/rails/autoloading"
@@ -0,0 +1,3 @@
1
+ Trailblazer::Operation.class_eval do
2
+ autoload :Responder, "trailblazer/operation/responder"
3
+ end
@@ -1,5 +1,5 @@
1
1
  module Trailblazer
2
2
  module Rails
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -17,10 +17,19 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency "trailblazer", ">= 1.0.0.rc1"
20
+ spec.add_dependency "trailblazer", ">= 1.0.0"
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.10"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "minitest"
25
25
  spec.add_development_dependency "rails"
26
+ spec.add_development_dependency "actionpack", '>= 3.0.0' # Rails is optional.
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "responders"
29
+ spec.add_development_dependency "database_cleaner"
30
+ spec.add_development_dependency "sidekiq", ">= 3.1.0"
31
+
32
+ spec.add_development_dependency "multi_json"
33
+
34
+
26
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailblazer-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-18 00:00:00.000000000 Z
11
+ date: 2015-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trailblazer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0.rc1
19
+ version: 1.0.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
- version: 1.0.0.rc1
26
+ version: 1.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +80,90 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: actionpack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
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: responders
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: database_cleaner
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
+ - !ruby/object:Gem::Dependency
140
+ name: sidekiq
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 3.1.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 3.1.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: multi_json
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
83
167
  description:
84
168
  email:
85
169
  - apotonick@gmail.com
@@ -89,12 +173,17 @@ extra_rdoc_files: []
89
173
  files:
90
174
  - ".gitignore"
91
175
  - ".travis.yml"
176
+ - CHANGES.md
92
177
  - Gemfile
93
178
  - LICENSE.txt
94
179
  - README.md
95
180
  - Rakefile
181
+ - lib/trailblazer/operation/controller/active_record.rb
96
182
  - lib/trailblazer/operation/model/active_model.rb
183
+ - lib/trailblazer/operation/responder.rb
184
+ - lib/trailblazer/operation/worker.rb
97
185
  - lib/trailblazer/rails.rb
186
+ - lib/trailblazer/rails/autoloading.rb
98
187
  - lib/trailblazer/rails/railtie.rb
99
188
  - lib/trailblazer/rails/version.rb
100
189
  - trailblazer-rails.gemspec