backframe 0.0.49 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +16 -0
- data/.gitignore +2 -2
- data/.rspec +2 -0
- data/.rubocop.yml +1156 -0
- data/Gemfile +9 -13
- data/README.md +133 -4
- data/Rakefile +3 -7
- data/backframe.gemspec +10 -11
- data/circle.yml +5 -2
- data/lib/backframe.rb +21 -34
- data/lib/backframe/mime.rb +4 -0
- data/lib/backframe/query.rb +48 -0
- data/lib/backframe/query/sort.rb +29 -0
- data/lib/backframe/railtie.rb +1 -16
- data/lib/backframe/response.rb +60 -0
- data/lib/backframe/response/adapter/csv.rb +39 -0
- data/lib/backframe/response/adapter/json.rb +53 -0
- data/lib/backframe/response/adapter/xlsx.rb +41 -0
- data/lib/backframe/response/adapter/xml.rb +37 -0
- data/lib/backframe/response/collection.rb +43 -0
- data/lib/backframe/response/fields.rb +62 -0
- data/lib/backframe/response/record.rb +38 -0
- data/lib/backframe/service.rb +60 -0
- data/lib/backframe/service/result/base.rb +24 -0
- data/lib/backframe/service/result/failure.rb +21 -0
- data/lib/backframe/service/result/success.rb +21 -0
- data/lib/backframe/version.rb +1 -1
- data/spec/fixtures/active_record.rb +22 -0
- data/spec/fixtures/models.rb +25 -0
- data/spec/fixtures/queries.rb +28 -0
- data/spec/fixtures/seeds.rb +12 -0
- data/spec/fixtures/serializers.rb +23 -0
- data/spec/fixtures/services.rb +26 -0
- data/spec/query/sort_spec.rb +47 -0
- data/spec/query_spec.rb +63 -0
- data/spec/response/adapter/csv_spec.rb +47 -0
- data/spec/response/adapter/json_spec.rb +66 -0
- data/spec/response/adapter/xlsx_spec.rb +59 -0
- data/spec/response/adapter/xml_spec.rb +63 -0
- data/spec/response/fields_spec.rb +45 -0
- data/spec/response/record_spec.rb +45 -0
- data/spec/response_spec.rb +153 -0
- data/spec/service/result/failure_spec.rb +16 -0
- data/spec/service/result/sucess_spec.rb +17 -0
- data/spec/service_spec.rb +16 -0
- data/spec/spec_helper.rb +15 -52
- metadata +78 -81
- data/lib/backframe/actioncontroller/acts_as_activation.rb +0 -70
- data/lib/backframe/actioncontroller/acts_as_api.rb +0 -39
- data/lib/backframe/actioncontroller/acts_as_api/adapter.rb +0 -53
- data/lib/backframe/actioncontroller/acts_as_api/errors.rb +0 -48
- data/lib/backframe/actioncontroller/acts_as_api/headers.rb +0 -11
- data/lib/backframe/actioncontroller/acts_as_api/page.rb +0 -181
- data/lib/backframe/actioncontroller/acts_as_reset.rb +0 -86
- data/lib/backframe/actioncontroller/acts_as_resource.rb +0 -92
- data/lib/backframe/actioncontroller/acts_as_resource/actions.rb +0 -100
- data/lib/backframe/actioncontroller/acts_as_session.rb +0 -80
- data/lib/backframe/activerecord/acts_as_activable.rb +0 -50
- data/lib/backframe/activerecord/acts_as_distinct.rb +0 -49
- data/lib/backframe/activerecord/acts_as_enum.rb +0 -62
- data/lib/backframe/activerecord/acts_as_orderable.rb +0 -40
- data/lib/backframe/activerecord/acts_as_percent.rb +0 -46
- data/lib/backframe/activerecord/acts_as_phone.rb +0 -59
- data/lib/backframe/activerecord/acts_as_user.rb +0 -101
- data/lib/backframe/activerecord/default_values.rb +0 -32
- data/lib/backframe/activerecord/filter_sort.rb +0 -79
- data/lib/backframe/activerecord/migration.rb +0 -25
- data/lib/backframe/image_cache/image_cache.rb +0 -45
- data/lib/backframe/image_cache/lib/asset.rb +0 -109
- data/lib/backframe/image_cache/lib/cache.rb +0 -67
- data/lib/backframe/image_cache/lib/conversions.rb +0 -132
- data/lib/backframe/models/activation.rb +0 -60
- data/lib/backframe/models/activity.rb +0 -40
- data/lib/backframe/models/reset.rb +0 -59
- data/lib/backframe/models/story.rb +0 -9
- data/lib/backframe/serializers/activity_serializer.rb +0 -44
- data/spec/backframe/acts_as_api_spec.rb +0 -225
- data/spec/backframe/acts_as_resource_spec.rb +0 -178
- data/spec/support/example_factory.rb +0 -9
- data/spec/support/example_serializer.rb +0 -3
- data/spec/support/schema.rb +0 -8
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Response
|
6
|
+
|
7
|
+
module Adapter
|
8
|
+
|
9
|
+
class Csv
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def render(collection, fields, separator = ",")
|
14
|
+
records = []
|
15
|
+
labels = []
|
16
|
+
fields.array.each do |field|
|
17
|
+
labels << field[:label]
|
18
|
+
end
|
19
|
+
records << labels.join(separator)
|
20
|
+
collection.records.each do |item|
|
21
|
+
serialized = ActiveModelSerializers::SerializableResource.new(item).serializable_hash
|
22
|
+
record = []
|
23
|
+
fields.array.each do |field|
|
24
|
+
record << Backframe::Response::Record.get_value(serialized, field[:key])
|
25
|
+
end
|
26
|
+
records << record.join(separator)
|
27
|
+
end
|
28
|
+
records.join("\n")
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Response
|
6
|
+
|
7
|
+
module Adapter
|
8
|
+
|
9
|
+
class Json
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def render(collection, fields)
|
14
|
+
data = {
|
15
|
+
records: [],
|
16
|
+
total_records: collection.total_records,
|
17
|
+
current_page: collection.current_page,
|
18
|
+
total_pages: collection.total_pages
|
19
|
+
}
|
20
|
+
collection.records.each do |item|
|
21
|
+
serialized = ActiveModelSerializers::SerializableResource.new(item).serializable_hash
|
22
|
+
if fields.any?
|
23
|
+
record = {}
|
24
|
+
fields.array.each do |field|
|
25
|
+
obj = record
|
26
|
+
parts = field[:key].split(".")
|
27
|
+
value = Backframe::Response::Record.get_value(serialized, field[:key])
|
28
|
+
parts.each_with_index do |part, index|
|
29
|
+
if index == parts.size - 1
|
30
|
+
obj[part.to_sym] = value
|
31
|
+
else
|
32
|
+
obj[part.to_sym] ||= {}
|
33
|
+
obj = obj[part.to_sym]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
data[:records] << record
|
38
|
+
else
|
39
|
+
data[:records] << serialized
|
40
|
+
end
|
41
|
+
end
|
42
|
+
data
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Response
|
6
|
+
|
7
|
+
module Adapter
|
8
|
+
|
9
|
+
class Xlsx
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def render(collection, fields)
|
14
|
+
filename = SecureRandom.hex(32).to_s.upcase[0,16]
|
15
|
+
workbook = WriteXLSX.new(filename)
|
16
|
+
worksheet = workbook.add_worksheet
|
17
|
+
fields.array.each_with_index do |field, i|
|
18
|
+
worksheet.write_string(0, i, field[:label])
|
19
|
+
end
|
20
|
+
collection.records.each_with_index do |item, i|
|
21
|
+
serialized = ActiveModelSerializers::SerializableResource.new(item).serializable_hash
|
22
|
+
fields.array.each_with_index do |field, j|
|
23
|
+
value = Backframe::Response::Record.get_value(serialized, field[:key])
|
24
|
+
worksheet.write_string((i + 1), j, value.to_s)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
workbook.close
|
28
|
+
output = open(filename).read
|
29
|
+
File.unlink(filename)
|
30
|
+
output
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Response
|
6
|
+
|
7
|
+
module Adapter
|
8
|
+
|
9
|
+
class Xml
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def render(collection, fields)
|
14
|
+
output = '<?xml version="1.0"?>'
|
15
|
+
output += "<records>"
|
16
|
+
collection.records.each do |item|
|
17
|
+
serialized = ActiveModelSerializers::SerializableResource.new(item).serializable_hash
|
18
|
+
output += "<record>"
|
19
|
+
fields.array.each do |field|
|
20
|
+
value = Backframe::Response::Record.get_value(serialized, field[:key])
|
21
|
+
output += "<#{field[:key]}>#{value}</#{field[:key]}>"
|
22
|
+
end
|
23
|
+
output += "</record>"
|
24
|
+
end
|
25
|
+
output += "</records>"
|
26
|
+
output
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Response
|
6
|
+
|
7
|
+
class Collection
|
8
|
+
|
9
|
+
def initialize(collection, page, per_page)
|
10
|
+
@collection = collection
|
11
|
+
@page = page
|
12
|
+
@per_page = per_page
|
13
|
+
end
|
14
|
+
|
15
|
+
def total_records
|
16
|
+
@collection.count
|
17
|
+
end
|
18
|
+
|
19
|
+
def total_pages
|
20
|
+
@page ? (total_records / @per_page.to_f).ceil : 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def current_page
|
24
|
+
@page ? [total_pages, @page.to_i].min : 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def limit
|
28
|
+
@page ? @per_page : total_records
|
29
|
+
end
|
30
|
+
|
31
|
+
def offset
|
32
|
+
@page ? limit * (@page - 1) : 0
|
33
|
+
end
|
34
|
+
|
35
|
+
def records
|
36
|
+
@page ? @collection.limit(limit).offset(offset) : @collection.all
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Response
|
6
|
+
|
7
|
+
class Fields
|
8
|
+
|
9
|
+
def initialize(collection, fields)
|
10
|
+
@collection = collection
|
11
|
+
@fields = fields
|
12
|
+
end
|
13
|
+
|
14
|
+
def any?
|
15
|
+
!@fields.nil?
|
16
|
+
end
|
17
|
+
|
18
|
+
def array
|
19
|
+
return @array if defined?(@array)
|
20
|
+
hash = ActiveModelSerializers::SerializableResource.new(@collection.first).serializable_hash
|
21
|
+
keys = keys(hash)
|
22
|
+
@array = []
|
23
|
+
if @fields.nil?
|
24
|
+
keys.each do |key|
|
25
|
+
@array << { label: key, key: key }
|
26
|
+
end
|
27
|
+
else
|
28
|
+
@fields.split(",").each do |token|
|
29
|
+
field = nil
|
30
|
+
if token =~ /([\w\s]*):([\w\s\.]*)/
|
31
|
+
field = { label: $1.strip, key: $2.strip }
|
32
|
+
elsif token =~ /([\w\s\.]*)/
|
33
|
+
field = { label: $1.strip, key: $1.strip }
|
34
|
+
end
|
35
|
+
if keys.include?(field[:key])
|
36
|
+
@array << field
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
@array
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def keys(hash, prefix = '')
|
46
|
+
keys = []
|
47
|
+
hash.each do |key, value|
|
48
|
+
fullkey = (!prefix.empty?) ? "#{prefix}.#{key}" : key
|
49
|
+
if value.is_a?(Hash)
|
50
|
+
keys.concat(keys(value, fullkey))
|
51
|
+
else
|
52
|
+
keys << fullkey.to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
keys
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Response
|
6
|
+
|
7
|
+
class Record
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def get_value(record, path)
|
12
|
+
path = cast_path(path)
|
13
|
+
index = 0
|
14
|
+
length = path.length
|
15
|
+
while record != nil && index < length
|
16
|
+
record = record.with_indifferent_access[path[index]]
|
17
|
+
index += 1
|
18
|
+
end
|
19
|
+
record
|
20
|
+
end
|
21
|
+
|
22
|
+
def cast_path(path)
|
23
|
+
if path.is_a?(String)
|
24
|
+
path.split(".")
|
25
|
+
elsif path.is_a?(Symbol)
|
26
|
+
path.to_s.split(".")
|
27
|
+
elsif path.is_a?(Array)
|
28
|
+
path
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Service
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def build(*args)
|
10
|
+
new(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform(*args)
|
14
|
+
service = build(*args)
|
15
|
+
|
16
|
+
message = nil
|
17
|
+
errors = {}
|
18
|
+
result = nil
|
19
|
+
|
20
|
+
ActiveRecord::Base.transaction do
|
21
|
+
begin
|
22
|
+
result = service.perform
|
23
|
+
rescue StandardError => e
|
24
|
+
message = e.message
|
25
|
+
# errors = e.errors
|
26
|
+
service.before_rollback
|
27
|
+
raise ActiveRecord::Rollback
|
28
|
+
end
|
29
|
+
service.before_commit
|
30
|
+
end
|
31
|
+
|
32
|
+
if message.nil?
|
33
|
+
service.after_commit
|
34
|
+
else
|
35
|
+
service.after_rollback
|
36
|
+
end
|
37
|
+
|
38
|
+
return (message.present?) ? Result::Failure.new(message: message, errors: errors) : Result::Success.new(result)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
def perform
|
44
|
+
end
|
45
|
+
|
46
|
+
def before_rollback
|
47
|
+
end
|
48
|
+
|
49
|
+
def after_rollback
|
50
|
+
end
|
51
|
+
|
52
|
+
def before_commit
|
53
|
+
end
|
54
|
+
|
55
|
+
def after_commit
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Backframe
|
4
|
+
|
5
|
+
class Service
|
6
|
+
|
7
|
+
module Result
|
8
|
+
|
9
|
+
class Base
|
10
|
+
|
11
|
+
def initialize(*args)
|
12
|
+
args[0].each do |key, val|
|
13
|
+
self.class.send(:attr_accessor, key)
|
14
|
+
self.instance_variable_set("@#{key}", val)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|