lca 0.2 → 0.2.4

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
  SHA256:
3
- metadata.gz: 4ff437267879f30b1fdf073964e93eaf6be5c580a8baefa2fd308b1b7d38de1f
4
- data.tar.gz: 6ffcf161a6bd74b41f3ca07ae7ce78e41e8e9fa68f1ff1c5350a1bab5193c9d0
3
+ metadata.gz: fc53882ad2b8c5c34b869be0546546973dd829b25aa39f354e9be9da63a44fae
4
+ data.tar.gz: 57c65eeadf05e7e6f160ea1c2ca3db8a025614578ecb6242d9f1404002651766
5
5
  SHA512:
6
- metadata.gz: a2133b1c9680cb1cfb78b26335d449a86b04871fac6357f2a1741dc57434398c40156af1613a691485b168d0cfb62ee41f0c8299829b5594ba6eebfe218499c4
7
- data.tar.gz: 65a355dd57bdaf6deffcdffbb567247b6d28a6ac1dd7100631205e4d1337cf123c110aeeb517372ade19f13f166c85c9ee837d25c7441a692942d988cd946b8e
6
+ metadata.gz: 2a1542ddf863b9461fb3ceb67e45f59599608c40e98994203918b7aeb4fb87ef2e4b1f73bd7fa3a837a0cd9cbd691cf72648505f38f156c64b84b6ee097d0dd6
7
+ data.tar.gz: e7989620c9b0a81249e741b41bcbe8fd1854b24650d66e4963ce940ab593d8ea2340403f06f467d8e280cea3dfed99d1c5067407771557403bf2d049cf792b0c
data/Gemfile CHANGED
@@ -9,4 +9,7 @@ gem "rspec", "~> 3.0"
9
9
 
10
10
  gem "activerecord", "~> 6.0"
11
11
  gem "activestorage", "~> 6.0"
12
- gem "actiontext", "~> 6.0"
12
+ gem "actiontext", "~> 6.0"
13
+
14
+ gem "jwt"
15
+ gem "pg_ltree"
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/lca.svg)](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
- * more models for various LCA cycles, processes and impacts
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, including postgres RECURSIVE queries
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
- copy_file "config.yml.tt", "config/lca.yml"
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"
@@ -8,7 +8,8 @@ LCA_OPTIONS ||= begin
8
8
  create_postgrest_roles: true,
9
9
  jwt_secret: "supersecret",
10
10
  jwt_encryption: "HS256",
11
- destroy_partition_on_owner_destroy: true
11
+ destroy_partition_on_owner_destroy: true,
12
+ owner_role_suffix: "changeme"
12
13
  }
13
14
  end
14
15
  end.merge({
@@ -22,8 +22,8 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
22
22
  type text,
23
23
  name text,
24
24
 
25
- parent_id integer,
26
- parent_type text,
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], :parent_id
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::Cycle", foreign_key: :owner_id, as: :owner
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 lca_owner_#{id}"
46
+ lca_sql "drop role if exists #{ lca_role }"
47
47
 
48
48
  # create role
49
- lca_sql "create role lca_owner_#{id}"
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 lca_owner_#{id}"
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 lca_owner_#{id}"
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 lca_owner_#{id}"
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 lca_owner_#{id}"
74
+ lca_sql "alter table #{lca_table_name} detach partition #{ lca_role }"
75
75
  end
76
76
  end # delete_storage
77
77
 
@@ -1,6 +1,6 @@
1
1
  class Lca::Cycle < Lca::Model
2
2
 
3
- has_many :stages, class_name: "::Lca::Stage", foreign_key: :parent_id, dependent: :destroy
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}.*")
@@ -1,4 +1,4 @@
1
1
  class Lca::Exchange < Lca::Model
2
- belongs_to :process, class_name: "::Lca::Process", foreign_key: :parent_id
3
- has_many :impacts, class_name: "::Lca::Impact", foreign_key: :parent_id, dependent: :destroy
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
@@ -1,6 +1,6 @@
1
1
  class Lca::Impact < Lca::Model
2
- belongs_to :exchange, class_name: "::Lca::Exchange", foreign_key: :parent_id
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
@@ -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
- before_save :set_defaults
24
+ before_validation :set_defaults
23
25
  def set_defaults
24
- self.path ||= name.parameterize.gsub("-", ".")
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
@@ -1,10 +1,9 @@
1
1
  class Lca::Process < Lca::Model
2
2
 
3
- has_many :exchanges, class_name: "::Lca::Exchange", foreign_key: :parent_id, dependent: :destroy
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
- def subprocesses
8
- ::Lca::Process.where("path ~ ?", "#{path}.*")
9
- end
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
@@ -1,6 +1,6 @@
1
1
  class Lca::Stage < Lca::Model
2
2
 
3
- belongs_to :cycle, class_name: "::Lca::Cycle", foreign_key: :parent_id
4
- has_many :processes, class_name: "::Lca::Process", foreign_key: :parent_id
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lca
4
- VERSION = "0.2"
4
+ VERSION = "0.2.4"
5
5
  end
data/lib/lca.rb CHANGED
@@ -3,6 +3,8 @@ require "active_support/all"
3
3
 
4
4
  require "active_record"
5
5
 
6
+ require "pg_ltree"
7
+
6
8
  require_relative "lca/version"
7
9
 
8
10
  require_relative "lca/models/concerns/statusable"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lca
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick @ Earthster