lca 0.2 → 0.2.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.
- checksums.yaml +4 -4
- data/Gemfile +4 -1
- data/README.md +6 -3
- data/lib/generators/lca/install_generator.rb +4 -1
- data/lib/generators/lca/templates/config.yml.tt +4 -0
- data/lib/generators/lca/templates/initializer.rb.tt +2 -1
- data/lib/generators/lca/templates/migration.rb.tt +3 -3
- data/lib/lca/models/concerns/lcable.rb +8 -8
- data/lib/lca/models/cycle.rb +1 -1
- data/lib/lca/models/exchange.rb +2 -2
- data/lib/lca/models/impact.rb +2 -2
- data/lib/lca/models/model.rb +10 -4
- data/lib/lca/models/process.rb +4 -5
- data/lib/lca/models/stage.rb +2 -2
- data/lib/lca/version.rb +1 -1
- data/lib/lca.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc53882ad2b8c5c34b869be0546546973dd829b25aa39f354e9be9da63a44fae
|
4
|
+
data.tar.gz: 57c65eeadf05e7e6f160ea1c2ca3db8a025614578ecb6242d9f1404002651766
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a1542ddf863b9461fb3ceb67e45f59599608c40e98994203918b7aeb4fb87ef2e4b1f73bd7fa3a837a0cd9cbd691cf72648505f38f156c64b84b6ee097d0dd6
|
7
|
+
data.tar.gz: e7989620c9b0a81249e741b41bcbe8fd1854b24650d66e4963ce940ab593d8ea2340403f06f467d8e280cea3dfed99d1c5067407771557403bf2d049cf792b0c
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[](https://badge.fury.io/rb/lca)
|
2
|
+
|
1
3
|
# LCA
|
2
4
|
|
3
5
|
Storing, processing and working with life-cycle assessment data has always been challenging. A multitude of data models and implementations exist already but every one of them makes huge compromises or lacks functionality.
|
@@ -88,12 +90,13 @@ If the `create_postgrest_roles` setting is on each new owner will be assigned a
|
|
88
90
|
|
89
91
|
## Roadmap
|
90
92
|
|
91
|
-
*
|
93
|
+
* .child / .parent query support across models unrelated through inheritance
|
94
|
+
* more model templates for various LCA cycles, processes and impacts
|
92
95
|
* model validations
|
93
|
-
* query objects
|
96
|
+
* query objects
|
97
|
+
* postgres RECURSIVE queries
|
94
98
|
* builders
|
95
99
|
* seeds/fixtures
|
96
|
-
* automated tests
|
97
100
|
|
98
101
|
|
99
102
|
## Contributing
|
@@ -15,7 +15,10 @@ module Lca
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def copy_config
|
18
|
-
|
18
|
+
conf_file = "config/lca.yml"
|
19
|
+
copy_file "config.yml.tt", conf_file
|
20
|
+
contents = File.read( conf_file ).gsub("changeme", ('a'..'z').to_a.shuffle.first(4).join )
|
21
|
+
File.open(conf_file, 'wb') { |file| file.write(contents) }
|
19
22
|
end
|
20
23
|
|
21
24
|
def migration_version
|
@@ -12,3 +12,7 @@ destroy_partition_on_owner_destroy: true
|
|
12
12
|
# jwt secret required for postgrest role switching
|
13
13
|
jwt_secret: "supersecret"
|
14
14
|
jwt_encryption: "HS256"
|
15
|
+
|
16
|
+
# suffix postgres roles with a random string
|
17
|
+
# to avoid collisions between other LCA installations in other apps using same db server
|
18
|
+
owner_role_suffix: "changeme"
|
@@ -22,8 +22,8 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
|
|
22
22
|
type text,
|
23
23
|
name text,
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
parent_entity_id integer,
|
26
|
+
parent_entity_type text,
|
27
27
|
|
28
28
|
path ltree,
|
29
29
|
path_slug text,
|
@@ -44,7 +44,7 @@ SQL
|
|
44
44
|
add_index LCA_OPTIONS[:table_name], :id
|
45
45
|
add_index LCA_OPTIONS[:table_name], :owner_id
|
46
46
|
add_index LCA_OPTIONS[:table_name], :type
|
47
|
-
add_index LCA_OPTIONS[:table_name], :
|
47
|
+
add_index LCA_OPTIONS[:table_name], :parent_entity_id
|
48
48
|
|
49
49
|
# next two indexes unfortunately can't be unique since a cycle can appear several times under an owner
|
50
50
|
add_index LCA_OPTIONS[:table_name], :path, using: :gist
|
@@ -12,13 +12,13 @@ module Lca
|
|
12
12
|
|
13
13
|
included do
|
14
14
|
|
15
|
-
has_many :lca_cycles, class_name: "::Lca::
|
15
|
+
has_many :lca_cycles, class_name: "::Lca::Model", foreign_key: :owner_id, as: :owner
|
16
16
|
after_create :lca_create_storage
|
17
17
|
before_destroy :lca_delete_storage
|
18
18
|
|
19
19
|
# instance methods
|
20
20
|
def lca_role
|
21
|
-
"lca_owner_#{id}"
|
21
|
+
"lca_owner_#{id}_#{ LCA_OPTIONS[:owner_role_suffix] }"
|
22
22
|
end # role
|
23
23
|
|
24
24
|
# Generates a JWT token the client (SPA) can pass to PostgREST for privilege escalation
|
@@ -43,13 +43,13 @@ module Lca
|
|
43
43
|
|
44
44
|
if LCA_OPTIONS[:create_postgrest_roles]
|
45
45
|
# drop role if it exists
|
46
|
-
lca_sql "drop role if exists
|
46
|
+
lca_sql "drop role if exists #{ lca_role }"
|
47
47
|
|
48
48
|
# create role
|
49
|
-
lca_sql "create role
|
49
|
+
lca_sql "create role #{ lca_role }"
|
50
50
|
|
51
51
|
# grant privs
|
52
|
-
lca_sql "grant all privileges on #{lca_table_name}_#{id} to
|
52
|
+
lca_sql "grant all privileges on #{lca_table_name}_#{id} to #{ lca_role }"
|
53
53
|
end
|
54
54
|
|
55
55
|
end # create_storage
|
@@ -60,10 +60,10 @@ module Lca
|
|
60
60
|
|
61
61
|
if LCA_OPTIONS[:create_postgrest_roles]
|
62
62
|
# revoke privs
|
63
|
-
lca_sql "REVOKE ALL PRIVILEGES ON #{lca_table_name}_#{id} FROM
|
63
|
+
lca_sql "REVOKE ALL PRIVILEGES ON #{lca_table_name}_#{id} FROM #{ lca_role }"
|
64
64
|
|
65
65
|
# delete role
|
66
|
-
lca_sql "drop role
|
66
|
+
lca_sql "drop role #{ lca_role }"
|
67
67
|
end
|
68
68
|
|
69
69
|
if LCA_OPTIONS[:destroy_partition_on_owner_destroy]
|
@@ -71,7 +71,7 @@ module Lca
|
|
71
71
|
lca_sql "drop table if exists #{lca_table_name}_#{id}"
|
72
72
|
else
|
73
73
|
# detach and forget about it
|
74
|
-
lca_sql "alter table #{lca_table_name} detach partition
|
74
|
+
lca_sql "alter table #{lca_table_name} detach partition #{ lca_role }"
|
75
75
|
end
|
76
76
|
end # delete_storage
|
77
77
|
|
data/lib/lca/models/cycle.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Lca::Cycle < Lca::Model
|
2
2
|
|
3
|
-
has_many :stages, class_name: "::Lca::Stage", foreign_key: :
|
3
|
+
has_many :stages, class_name: "::Lca::Stage", foreign_key: :parent_entity_id, dependent: :destroy
|
4
4
|
|
5
5
|
def subcycles
|
6
6
|
::Lca::Cycle.match_path("#{path}.*")
|
data/lib/lca/models/exchange.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
class Lca::Exchange < Lca::Model
|
2
|
-
belongs_to :process, class_name: "::Lca::Process", foreign_key: :
|
3
|
-
has_many :impacts, class_name: "::Lca::Impact", foreign_key: :
|
2
|
+
belongs_to :process, class_name: "::Lca::Process", foreign_key: :parent_entity_id, required: true
|
3
|
+
has_many :impacts, class_name: "::Lca::Impact", foreign_key: :parent_entity_id, dependent: :destroy
|
4
4
|
end
|
data/lib/lca/models/impact.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Lca::Impact < Lca::Model
|
2
|
-
belongs_to :exchange, class_name: "::Lca::Exchange", foreign_key: :
|
3
|
-
|
2
|
+
belongs_to :exchange, class_name: "::Lca::Exchange", foreign_key: :parent_entity_id, required: true
|
3
|
+
|
4
4
|
validates_presence_of :impact_amount
|
5
5
|
validates_presence_of :impact_amount_unit, allow_blank: false
|
6
6
|
validates_presence_of :impact_factor
|
data/lib/lca/models/model.rb
CHANGED
@@ -4,12 +4,14 @@ class Lca::Model < ActiveRecord::Base
|
|
4
4
|
|
5
5
|
self.primary_key = :id
|
6
6
|
|
7
|
+
ltree :path
|
8
|
+
|
7
9
|
def self.table_name
|
8
10
|
return ::LCA_OPTIONS[:table_name] if defined? ::LCA_OPTIONS
|
9
11
|
return "lca_models"
|
10
12
|
end
|
11
13
|
|
12
|
-
belongs_to :owner, polymorphic: :true
|
14
|
+
belongs_to :owner, polymorphic: :true, required: true
|
13
15
|
|
14
16
|
scope :match_path, -> (some_path) { where("path ~ ?", "#{some_path}") }
|
15
17
|
|
@@ -19,10 +21,10 @@ class Lca::Model < ActiveRecord::Base
|
|
19
21
|
validates_presence_of :name, allow_blank: false
|
20
22
|
validates_presence_of :path, allow_blank: false
|
21
23
|
|
22
|
-
|
24
|
+
before_validation :set_defaults
|
23
25
|
def set_defaults
|
24
|
-
self.path ||= name.
|
25
|
-
self.path_slug = path.parameterize
|
26
|
+
self.path ||= name.delete(" ").gsub(/[^0-9a-z ]/i, '') if name
|
27
|
+
self.path_slug = path.parameterize if path
|
26
28
|
end
|
27
29
|
|
28
30
|
|
@@ -57,6 +59,10 @@ class Lca::Model < ActiveRecord::Base
|
|
57
59
|
class_name
|
58
60
|
end
|
59
61
|
|
62
|
+
model_class.define_singleton_method(:sti_name) do
|
63
|
+
original_class_name
|
64
|
+
end
|
65
|
+
|
60
66
|
# override the STI name lmfao
|
61
67
|
model_class.define_singleton_method(:find_sti_class) do |p|
|
62
68
|
original_class_name.constantize
|
data/lib/lca/models/process.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
class Lca::Process < Lca::Model
|
2
2
|
|
3
|
-
has_many :exchanges, class_name: "::Lca::Exchange", foreign_key: :
|
3
|
+
has_many :exchanges, class_name: "::Lca::Exchange", foreign_key: :parent_entity_id, dependent: :destroy
|
4
4
|
has_many :impacts, through: :exchanges
|
5
|
-
belongs_to :stage, class_name: "::Lca::Stage", foreign_key: :parent_id
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
has_many :subprocesses, class_name: "::Lca::Process", foreign_key: :parent_entity_id, dependent: :destroy, as: :parent_entity
|
7
|
+
belongs_to :parent_entity, polymorphic: true, required: true
|
8
|
+
|
10
9
|
end
|
data/lib/lca/models/stage.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Lca::Stage < Lca::Model
|
2
2
|
|
3
|
-
belongs_to :cycle, class_name: "::Lca::Cycle", foreign_key: :
|
4
|
-
has_many :processes, class_name: "::Lca::Process", foreign_key: :
|
3
|
+
belongs_to :cycle, class_name: "::Lca::Cycle", foreign_key: :parent_entity_id, required: true
|
4
|
+
has_many :processes, class_name: "::Lca::Process", foreign_key: :parent_entity_id
|
5
5
|
|
6
6
|
end
|
data/lib/lca/version.rb
CHANGED
data/lib/lca.rb
CHANGED