openstax_utilities 1.2.0 → 1.3.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Yzc2ZjE5YTExNmU0YjkzYWRmMTgwYzQyYjJjOWYzMDE5Nzk2OWI4ZQ==
4
+ MjA4NWY1YWY4YjIyMDBkYjAxODE0MDU4YjlmODE0ZTJjNjE5ZjkyMA==
5
5
  data.tar.gz: !binary |-
6
- NDdhMjA5NDJiOWM4YmM3MzNiNWE2NWE3Nzk5ZmRlYzJiY2FlOTZhOA==
6
+ OWQyYzJkYzYxZjczNTcwYTY2MzYxMmM3NzU0NzQ1ZjU4MWQ0YWQwOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzI4MzM2MDBlN2U2MmNiYzU1ODk3NTA5NmExOWJlZTkyOTdhOGQ0OGQxNWMx
10
- Nzg5Y2UxNDNhOTc5NWFkYzIwMDkzZDI5YjRjNzcwMWNiOWRlYWY2YjExMDE5
11
- YTViOWUzZTY5NDRiZDFmODJjZGY5MjllOGMxZmY4ZDJhNmM0NmY=
9
+ MjQ3YWU0MzlhMmQzZjg1OWQwMDhiZTk0MzJmYmU3MDkxN2EzMmZmNjFiMTBl
10
+ ODA2ZGFjMDMzNjQ0NTRkYTM0NzZmMjIxNDkzNWNmNzIxMTFlMTQ0ZGRmZWE2
11
+ MDY3NTQ2ZjAzZGUwNzU0NTk0MDAwNGJmMjI4YTAwNjdkN2YxODE=
12
12
  data.tar.gz: !binary |-
13
- N2NjNWY2MTQzMDNmZjRiMTYxM2U0MzQ4NTQxMzQ5Y2Q2MDdiYWQ4YzA4OTc4
14
- ZGEzODA2YTdiYmY5ZTlhNGE0MDhmNjk1NjFjNDVmYTkzOGU5MDRlMjQ3NzFi
15
- ZjRjMWFiNGUwZDg5Y2RmMDhlNGFjZTk3MWQwNWMwMDQ5ZTg2MzI=
13
+ YTVkNDM3MGE5MGI2Y2M2YmU1NTVhMGE4MTU1MmFkNjQ1ZDRmN2Q5OGYwMTFh
14
+ ZmU1NjA0OTg4YzdiMmMwMGMwMTVkNDkwNWM0MGE3ZjMwMjJhZDJkZjdkMzEy
15
+ NjFjYjhjMjQ2YTNmNGQ1NDQxNGI0MDgzNDdiYjIyMGIyN2U0OTA=
@@ -0,0 +1,126 @@
1
+ # Copyright 2011-2014 Rice University. Licensed under the Affero General Public
2
+ # License version 3 or later. See the COPYRIGHT file for details.
3
+
4
+ module OpenStax
5
+ module Utilities
6
+
7
+ module Roar
8
+
9
+ def self.included(base)
10
+ base.send :extend, ClassMethods
11
+ end
12
+
13
+ def get_representer(represent_with, model=nil)
14
+ return nil if represent_with.nil?
15
+ if represent_with.is_a? Proc
16
+ represent_with.call(model)
17
+ else
18
+ represent_with
19
+ end
20
+ end
21
+
22
+ def rest_get(model_klass, id, represent_with=nil)
23
+ @model = model_klass.find(id)
24
+ raise SecurityTransgression unless current_user.can_read?(@model)
25
+ respond_with @model, represent_with: get_representer(represent_with, @model)
26
+ end
27
+
28
+ def rest_update(model_klass, id, represent_with=nil)
29
+ @model = model_klass.find(id)
30
+ raise SecurityTransgression unless current_user.can_update?(@model)
31
+ consume!(@model, represent_with: get_representer(represent_with, @model))
32
+
33
+ if @model.save
34
+ head :no_content
35
+ else
36
+ render json: @model.errors, status: :unprocessable_entity
37
+ end
38
+ end
39
+
40
+ def rest_create(model_klass)
41
+ @model = model_klass.new()
42
+
43
+ # Unlike the implications of the representable README, "consume!" can
44
+ # actually make changes to the database. See http://goo.gl/WVLBqA.
45
+ # We do want to consume before checking the permissions so we can know
46
+ # what we're dealing with, but if user doesn't have permission we don't
47
+ # want to have changed the DB. Wrap in a transaction to protect ourselves.
48
+
49
+ model_klass.transaction do
50
+ consume!(@model)
51
+ raise SecurityTransgression unless current_user.can_create?(@model)
52
+ end
53
+
54
+ if @model.save
55
+ respond_with @model
56
+ else
57
+ render json: @model.errors, status: :unprocessable_entity
58
+ end
59
+ end
60
+
61
+ def rest_destroy(model_klass, id)
62
+ @model = model_klass.find(id)
63
+ raise SecurityTransgression unless current_user.can_destroy?(@model)
64
+
65
+ if @model.destroy
66
+ head :no_content
67
+ else
68
+ render json: @model.errors, status: :unprocessable_entity
69
+ end
70
+ end
71
+
72
+ def standard_sort(model_klass)
73
+ # take array of all IDs or hash of id => position,
74
+ # regardless build up an array of all IDs in the right order and pass those to sort
75
+
76
+ new_positions = params['newPositions']
77
+ return head :no_content if new_positions.length == 0
78
+
79
+ # Can't have duplicate positions or IDs
80
+ unique_ids = new_positions.collect{|np| np['id']}.uniq
81
+ unique_positions = new_positions.collect{|np| np['position']}.uniq
82
+
83
+ return head :bad_request if unique_ids.length != new_positions.length
84
+ return head :bad_request if unique_positions.length != new_positions.length
85
+
86
+ first = model_klass.where(:id => new_positions[0]['id']).first
87
+
88
+ return head :not_found if first.blank?
89
+
90
+ originalOrdered = first.me_and_peers.ordered.all
91
+
92
+ originalOrdered.each do |item|
93
+ raise SecurityTransgression unless item.send(:container_column) == originalOrdered[0].send(:container_column) \
94
+ if item.respond_to?(:container_column)
95
+ raise SecurityTransgression unless current_user.can_sort?(item)
96
+ end
97
+
98
+ originalOrderedIds = originalOrdered.collect{|sc| sc.id}
99
+
100
+ newOrderedIds = Array.new(originalOrderedIds.size)
101
+
102
+ new_positions.each do |newPosition|
103
+ id = newPosition['id'].to_i
104
+ newOrderedIds[newPosition['position']] = id
105
+ originalOrderedIds.delete(id)
106
+ end
107
+
108
+ ptr = 0
109
+ for oldId in originalOrderedIds
110
+ while !newOrderedIds[ptr].nil?; ptr += 1; end
111
+ newOrderedIds[ptr] = oldId
112
+ end
113
+
114
+ begin
115
+ model_klass.sort!(newOrderedIds)
116
+ rescue Exception => e
117
+ return head :internal_server_error
118
+ end
119
+
120
+ head :no_content
121
+ end
122
+
123
+ end
124
+
125
+ end
126
+ end
@@ -1,5 +1,5 @@
1
1
  module OpenStax
2
2
  module Utilities
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
@@ -18,6 +18,7 @@ require "openstax/utilities/network"
18
18
  require "openstax/utilities/action_list"
19
19
  require "openstax/utilities/acts_as_numberable"
20
20
  require "openstax/utilities/delegate_access_control"
21
+ require "openstax/utilities/roar"
21
22
 
22
23
  require "openstax/utilities/classy_helper"
23
24
  require "openstax/utilities/helpers/misc"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openstax_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Slavinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-21 00:00:00.000000000 Z
11
+ date: 2014-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -67,7 +67,6 @@ files:
67
67
  - lib/openstax/utilities/blocks/table_row_block.rb
68
68
  - lib/openstax/utilities/classy_helper.rb
69
69
  - lib/openstax/utilities/delegate_access_control.rb
70
- - lib/openstax/utilities/development.rb
71
70
  - lib/openstax/utilities/engine.rb
72
71
  - lib/openstax/utilities/enum.rb
73
72
  - lib/openstax/utilities/exceptions.rb
@@ -77,6 +76,7 @@ files:
77
76
  - lib/openstax/utilities/helpers/misc.rb
78
77
  - lib/openstax/utilities/helpers/partials.rb
79
78
  - lib/openstax/utilities/network.rb
79
+ - lib/openstax/utilities/roar.rb
80
80
  - lib/openstax/utilities/ruby.rb
81
81
  - lib/openstax/utilities/settings.rb
82
82
  - lib/openstax/utilities/text.rb
@@ -1,8 +0,0 @@
1
- module OpenStax
2
- module Utilities
3
- module Development
4
-
5
-
6
- end
7
- end
8
- end