lca 0.2.6 → 0.3.1

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: 3d5a7b8821030f1d77f41f6fda374ffa6179bf8f412355380dbabb17618d820f
4
- data.tar.gz: 1a16958b8df53caa76ade2b50ab6e8a3e522267ba16ae03c19da3667a6f13ea9
3
+ metadata.gz: dbc00e9a89b46ca94ce7dbf00c87d68a987b400b58a16418be51071b3180eb2d
4
+ data.tar.gz: ae540cccdddc511605242fa856867fc36449651d184ccf2b369d00830dada0a8
5
5
  SHA512:
6
- metadata.gz: a16076c63779cd5dd152e6c4a95a0a332274659648155ff69b61b2d9540e72f74a726155eeac7ae71287d634aceeb5227e2f174e7ff6b418ff2e72b2ec7c1704
7
- data.tar.gz: 194fa4db64be6e462c6be6dfc17eeb4acc7e4861778fd6b292830af20ef394e3a82db564a9199bea8f7fee75e884400058809540b3bc161a54bb007f6c1b1811
6
+ metadata.gz: 2001dc43f655a3f9ea335be918b622f5622d847f82476d4cbe35f3ab83afaf9d3b13002e392effef3561bf75102faf64b7f98495910ee37e46c42091e5e05314
7
+ data.tar.gz: abf1a1e98631805b82f872cf5c562eb84a3252e8b37722fe400b880690ba441272b5d95011581936b7b74ce297ef8d57cc22ac6cb9c3c3f553ac811850d01332
data/Gemfile CHANGED
@@ -8,8 +8,6 @@ gem "rake", "~> 13.0"
8
8
  gem "rspec", "~> 3.0"
9
9
 
10
10
  gem "activerecord", "~> 6.0"
11
- gem "activestorage", "~> 6.0"
12
- gem "actiontext", "~> 6.0"
13
-
11
+ gem "active_tree", "~> 0.3.1"
14
12
  gem "jwt"
15
13
  gem "pg_ltree"
data/Gemfile.lock CHANGED
@@ -1,87 +1,40 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lca (0.1.18)
5
- actiontext (~> 6.0)
4
+ lca (0.3)
5
+ active_tree (~> 0.3)
6
6
  activerecord (~> 6.0)
7
- activestorage (~> 6.0)
8
7
  jwt (~> 2.2.3)
9
8
  pg_ltree (~> 1.1.8)
10
9
 
11
10
  GEM
12
11
  remote: https://rubygems.org/
13
12
  specs:
14
- actionpack (6.1.4.1)
15
- actionview (= 6.1.4.1)
16
- activesupport (= 6.1.4.1)
17
- rack (~> 2.0, >= 2.0.9)
18
- rack-test (>= 0.6.3)
19
- rails-dom-testing (~> 2.0)
20
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
21
- actiontext (6.1.4.1)
22
- actionpack (= 6.1.4.1)
23
- activerecord (= 6.1.4.1)
24
- activestorage (= 6.1.4.1)
25
- activesupport (= 6.1.4.1)
26
- nokogiri (>= 1.8.5)
27
- actionview (6.1.4.1)
28
- activesupport (= 6.1.4.1)
29
- builder (~> 3.1)
30
- erubi (~> 1.4)
31
- rails-dom-testing (~> 2.0)
32
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
33
- activejob (6.1.4.1)
34
- activesupport (= 6.1.4.1)
35
- globalid (>= 0.3.6)
13
+ active_tree (0.3.1)
14
+ activerecord (~> 6.0)
15
+ jwt (~> 2.2.3)
16
+ pg_ltree (~> 1.1.8)
36
17
  activemodel (6.1.4.1)
37
18
  activesupport (= 6.1.4.1)
38
19
  activerecord (6.1.4.1)
39
20
  activemodel (= 6.1.4.1)
40
21
  activesupport (= 6.1.4.1)
41
- activestorage (6.1.4.1)
42
- actionpack (= 6.1.4.1)
43
- activejob (= 6.1.4.1)
44
- activerecord (= 6.1.4.1)
45
- activesupport (= 6.1.4.1)
46
- marcel (~> 1.0.0)
47
- mini_mime (>= 1.1.0)
48
22
  activesupport (6.1.4.1)
49
23
  concurrent-ruby (~> 1.0, >= 1.0.2)
50
24
  i18n (>= 1.6, < 2)
51
25
  minitest (>= 5.1)
52
26
  tzinfo (~> 2.0)
53
27
  zeitwerk (~> 2.3)
54
- builder (3.2.4)
55
28
  concurrent-ruby (1.1.9)
56
- crass (1.0.6)
57
29
  diff-lcs (1.4.4)
58
- erubi (1.10.0)
59
- globalid (0.5.2)
60
- activesupport (>= 5.0)
61
30
  i18n (1.8.10)
62
31
  concurrent-ruby (~> 1.0)
63
32
  jwt (2.2.3)
64
- loofah (2.12.0)
65
- crass (~> 1.0.2)
66
- nokogiri (>= 1.5.9)
67
- marcel (1.0.1)
68
- mini_mime (1.1.1)
69
33
  minitest (5.14.4)
70
- nokogiri (1.12.4-x86_64-linux)
71
- racc (~> 1.4)
72
34
  pg (1.2.3)
73
35
  pg_ltree (1.1.8)
74
36
  activerecord (>= 4.0.0, <= 7.0.0.rc1)
75
37
  pg (>= 0.17.0, < 2)
76
- racc (1.5.2)
77
- rack (2.2.3)
78
- rack-test (1.1.0)
79
- rack (>= 1.0, < 3)
80
- rails-dom-testing (2.0.3)
81
- activesupport (>= 4.2.0)
82
- nokogiri (>= 1.6)
83
- rails-html-sanitizer (1.4.2)
84
- loofah (~> 2.3)
85
38
  rake (13.0.6)
86
39
  rspec (3.10.0)
87
40
  rspec-core (~> 3.10.0)
@@ -104,12 +57,13 @@ PLATFORMS
104
57
  x86_64-linux
105
58
 
106
59
  DEPENDENCIES
107
- actiontext (~> 6.0)
60
+ active_tree (~> 0.3.1)
108
61
  activerecord (~> 6.0)
109
- activestorage (~> 6.0)
62
+ jwt
110
63
  lca!
64
+ pg_ltree
111
65
  rake (~> 13.0)
112
66
  rspec (~> 3.0)
113
67
 
114
68
  BUNDLED WITH
115
- 2.2.27
69
+ 2.2.29
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Gem Version](https://badge.fury.io/rb/lca.svg)](https://badge.fury.io/rb/lca)
1
+ [![Tests](https://github.com/nicksterious/lca/actions/workflows/ci.yml/badge.svg)](https://github.com/nicksterious/lca/actions/workflows/ci.yml) [![Ruby Gem](https://github.com/nicksterious/lca/actions/workflows/rubygems.yml/badge.svg)](https://github.com/nicksterious/lca/actions/workflows/rubygems.yml) [![Gem Version](https://badge.fury.io/rb/lca.svg)](https://badge.fury.io/rb/lca)
2
2
 
3
3
  # LCA
4
4
 
@@ -8,6 +8,8 @@ This gem implements a denormalized database model for life-cycle assessment data
8
8
 
9
9
  ## Installation
10
10
 
11
+ This gem is at home within a Rails 6+ console-only, API or full fledged application on top of a Postgres database.
12
+
11
13
  Add this line to your application's Gemfile:
12
14
 
13
15
  ```ruby
@@ -22,15 +24,24 @@ Or install it yourself as:
22
24
 
23
25
  $ gem install lca
24
26
 
25
- Upon installing the gem you must run the install process in your Rails application's root:
27
+ Upon installing the gem you must run the ActiveTree install process in your Rails application's root:
28
+
29
+ $ rails g active_tree:install
30
+
31
+
32
+ This will generate a `config/active_tree.yml` which you may customize to your needs, an initializer and a migration file which you can also customize to add any database columns your models will require.
33
+
34
+ You must also run the LCA gem install generator:
26
35
 
27
36
  $ rails g lca:install
28
37
 
29
- This will generate a `config/lca.yml` which you may customize to your needs, an initializer and a migration file which you can also customize to add any database columns your models will require.
38
+
39
+ This will create a migration file which you can edit to add your models' specific attributes.
40
+
30
41
 
31
42
  ## Usage
32
43
 
33
- Include the LCA concern into one of your models which will own lifecycle trees (owner model):
44
+ Include the ActiveTree concern into one of your models which will own lifecycle trees (owner model):
34
45
 
35
46
  ```ruby
36
47
  class User < ApplicationRecord
@@ -43,16 +54,16 @@ This will extend your model and enable the following functionality:
43
54
 
44
55
  ```ruby
45
56
  # query with ActiveRecord syntax
46
- User.last.lca_cycles
47
- User.find_by(name: "Acme").lca_cycles.impacts.select("impact_unit, sum(impact_amount) as total_impact").group(:impact_unit)
48
- User.last.lca_cycles.where(...)
49
- User.last.lca_cycles.where(...).group(...)
50
- User.last.lca_cycles.where(...).limit(...).offset(...)
57
+ User.last.active_trees
58
+ User.find_by(name: "Acme").active_trees.impacts.select("impact_unit, sum(impact_amount) as total_impact").group(:impact_unit)
59
+ User.last.active_trees.where(...)
60
+ User.last.active_trees.where(...).group(...)
61
+ User.last.active_trees.where(...).limit(...).offset(...)
51
62
 
52
63
  # AR query with ltree syntax
53
- User.last.lca_cycles.disabled.match_path("*.CustomProcess.*")
54
- User.last.lca_cycles.match_path("*{5,10}.CustomProcess.*.Ecosphere.*")
55
- User.last.lca_cycles.active.match_path("*.WhateverProcess.*.Ecosphere.*.CO2Emission.*").where( impact_amount: [100..150]).sum(:impact_amount)
64
+ User.last.active_trees.disabled.match_path("*.CustomProcess.*")
65
+ User.last.active_trees.match_path("*{5,10}.CustomProcess.*.Ecosphere.*")
66
+ User.last.active_trees.active.match_path("*.WhateverProcess.*.Ecosphere.*.CO2Emission.*").where( impact_amount: [100..150]).sum(:impact_amount)
56
67
 
57
68
  Lca::Cycle.match_path("Top.Electric Vehicle.*").impacts.match_path("*.CO2*").where( impact_amount: [ 1000..10000 ]).average(:impact_amount)
58
69
  Lca::Cycle::Product.where(name: "Electric Vehicle Battery").impacts.match_path("*.Cobalt.*").sum(:impact_amount)
@@ -62,11 +73,11 @@ Lca::Exchange.match_path("*.Oil.*.Ecosphere.*").impacts.sum(:impact_amount)
62
73
  Lca::Product.match_path("*.ElectricVehicle.*").processes.match_path("*.Processing.*").where(location: "EU").impacts.match_path("*.Lithium.*").where(location: ["CN", "Africa"]).sum(:impact_amount)
63
74
 
64
75
  # pg_ltree queries
65
- User.last.lca_cycles.last.parent
76
+ User.last.active_trees.last.parent
66
77
  Lca::Process.match_path("*.Manual assembly.*").children
67
78
 
68
79
  # pg_ltree combined with AR syntax
69
- User.last.lca_cycles(type: "Lca::Product").children.match_path("*.Retail").children.exchanges
80
+ User.last.active_trees(type: "Lca::Product").children.match_path("*.Retail").children.exchanges
70
81
 
71
82
  # all queries can be directed to a specific partition:
72
83
  Lca::Process.owned_by( owner_id ).match_path("*.Recycling.*").where(impact_unit: "tons CO2/year").impacts.sum(:impact_amount)
@@ -81,12 +92,13 @@ Lca::Impact::Ecosphere::Fauna.match_path("*.ResourceExtraction.*").where(impact_
81
92
  ```
82
93
 
83
94
  To see what syntax to use for path traversal please check out the following resources:
95
+ * active_tree gem https://github.com/nicksterious/active_tree
84
96
  * pg_ltree gem https://github.com/sjke/pg_ltree
85
97
  * Postgres ltree extension documentation https://www.postgresql.org/docs/9.1/ltree.html
86
98
 
87
- The LCA gem is designed to be compatible with PostgREST. PostgREST is an amazing tool that generates a CRUD REST API for your Postgres database, read more about it here: https://postgrest.org
99
+ ## Scalability
88
100
 
89
- If the `create_postgrest_roles` setting is on each new owner will be assigned a Postgres role allowing them to access data within their partition using PostgREST. Your owner model will be extended with a `.generate_jwt` method you can use to generate the PostgREST authentication token.
101
+ All LCA data can be stored into separate partitions using the ActiveTree partitioning feature. See `config/active_tree.yml` for table and partitioning naming and options.
90
102
 
91
103
  ## Caveats
92
104
 
@@ -108,7 +120,6 @@ Lca::Process.last.children.unscope(where: :type).impacts.where(impact_amount: 50
108
120
  * .child / .parent query support across models unrelated through inheritance
109
121
  * more model templates for various LCA cycles, processes and impacts
110
122
  * model validations
111
- * query objects
112
123
  * postgres RECURSIVE queries
113
124
  * builders
114
125
  * seeds/fixtures
@@ -118,10 +129,6 @@ Lca::Process.last.children.unscope(where: :type).impacts.where(impact_amount: 50
118
129
 
119
130
  Bug reports, pull requests and feature suggestions are welcome on GitHub at https://github.com/nicksterious/lca
120
131
 
121
- ## Sponsors
122
-
123
- We welcome any sponsorship through Github Sponsors. All corporate sponsors' logo and URL of choice shall show up within this section.
124
-
125
132
  ## License
126
133
 
127
134
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -10,17 +10,6 @@ module Lca
10
10
  migration_template "migration.rb.tt", "db/migrate/install_lca.rb", migration_version: migration_version
11
11
  end
12
12
 
13
- def copy_initializer
14
- copy_file 'initializer.rb.tt', 'config/initializers/lca.rb'
15
- end
16
-
17
- def copy_config
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) }
22
- end
23
-
24
13
  def migration_version
25
14
  "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
26
15
  end
@@ -1,81 +1,18 @@
1
1
  class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
3
 
4
- # enable ltree extension
5
- begin
6
- execute "create extension ltree"
7
- rescue
8
- p "LTREE was already enabled"
9
- end
10
-
11
-
12
- # create the main table as set up within LCA_OPTIONS[:table_name]
13
- execute <<-SQL
14
- create table #{ LCA_OPTIONS[:table_name] } (
15
- id serial,
16
-
17
- owner_id integer,
18
- owner_type character varying,
19
-
20
- status integer,
21
-
22
- data_external_id character varying,
23
- data_provider character varying,
24
-
25
- type character varying,
26
- name text,
27
-
28
- parent_entity_id integer,
29
- parent_entity_type character varying,
30
-
31
- path ltree,
32
- path_slug text,
33
-
34
- metadata_inline jsonb,
35
-
36
- impact_amount decimal,
37
- impact_amount_previous decimal,
38
- impact_amount_unit text,
39
- impact_factor decimal,
40
- impact_precision decimal,
41
-
42
- created_at timestamp(6) without time zone not null,
43
- updated_at timestamp(6) without time zone not null,
44
-
45
-
46
- primary key (id, owner_id)
47
- ) partition by list(owner_id)
48
- SQL
49
-
50
- # create an "others" partition for when the owner is undefined/unknown? just in case / may help in some edge cases
51
- execute "CREATE TABLE #{LCA_OPTIONS[:table_name]}_others PARTITION OF #{LCA_OPTIONS[:table_name]} DEFAULT"
52
-
53
-
54
- # add indexes
55
- add_index LCA_OPTIONS[:table_name], :id
56
- add_index LCA_OPTIONS[:table_name], :owner_id
57
- add_index LCA_OPTIONS[:table_name], :type
58
- add_index LCA_OPTIONS[:table_name], :parent_entity_id
59
-
60
- # next two indexes unfortunately can't be unique since a cycle can appear several times under an owner
61
- add_index LCA_OPTIONS[:table_name], :path, using: :gist
62
- add_index LCA_OPTIONS[:table_name], [:data_provider, :data_external_id]
63
-
64
-
65
- # when postgrest is enabled...
66
- if LCA_OPTIONS[:create_postgrest_roles]
67
- # create postgrest anon user with no privs
68
- # postgrest may pass an user's role using JWT
69
- execute "drop role if exists postgrest_anon"
70
- execute "create role postgrest_anon nologin"
71
- execute "grant postgrest_anon to #{ LCA_OPTIONS[:database_user] }"
72
- end
4
+ # add any columns required by your business model
5
+ add_column ACTIVE_TREE_OPTIONS[:table_name], :impact_amount, :decimal
6
+ add_column ACTIVE_TREE_OPTIONS[:table_name], :impact_amount_previous, :decimal
7
+ add_column ACTIVE_TREE_OPTIONS[:table_name], :impact_amount_unit, :text
8
+ add_column ACTIVE_TREE_OPTIONS[:table_name], :impact_factor, :decimal
9
+ add_column ACTIVE_TREE_OPTIONS[:table_name], :impact_precision, :decimal
73
10
 
74
11
 
75
12
  # snapshots table
76
13
  # maybe this should only store the impacts?
77
14
  execute <<-SQL
78
- create table #{ LCA_OPTIONS[:table_name] }_snapshots (
15
+ create table #{ ACTIVE_TREE_OPTIONS[:table_name] }_snapshots (
79
16
  id serial,
80
17
 
81
18
  impact_id integer,
@@ -97,22 +34,10 @@ SQL
97
34
  SQL
98
35
 
99
36
  # index the snapshots table
100
- add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :id
101
- add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :owner_id
102
- add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :impact_id
103
- add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :created_at
37
+ add_index "#{ACTIVE_TREE_OPTIONS[:table_name]}_snapshots", :id
38
+ add_index "#{ACTIVE_TREE_OPTIONS[:table_name]}_snapshots", :owner_id
39
+ add_index "#{ACTIVE_TREE_OPTIONS[:table_name]}_snapshots", :impact_id
40
+ add_index "#{ACTIVE_TREE_OPTIONS[:table_name]}_snapshots", :created_at
104
41
 
105
-
106
- # metadata table
107
- create_table "#{ LCA_OPTIONS[:table_name] }_metadata" do |t|
108
- t.string :model_type
109
- t.integer :model_id
110
- t.string :key
111
- t.text :value
112
-
113
- t.timestamps
114
- end
115
- add_index "#{ LCA_OPTIONS[:table_name] }_metadata", [ :model_type, :model_id ]
116
- add_index "#{ LCA_OPTIONS[:table_name] }_metadata", [ :key ]
117
42
  end
118
43
  end
@@ -7,82 +7,54 @@ module Lca
7
7
  extend ActiveSupport::Concern
8
8
 
9
9
  included do
10
+ before_destroy :lca_delete_storage
10
11
 
11
- has_many :lca_cycles, class_name: "::Lca::Model", foreign_key: :owner_id, as: :owner
12
- after_create :lca_create_storage
13
- before_destroy :lca_delete_storage
12
+ #
13
+ include ActiveTree::ActiveTreeAble
14
+ after_create :lca_create_storage
14
15
 
15
- # instance methods
16
- def lca_role
17
- "lca_owner_#{id}_#{ LCA_OPTIONS[:owner_role_suffix] }"
18
- end # role
16
+ has_many :lca_cycles, class_name: "::Lca::Model", foreign_key: :owner_id, as: :owner
19
17
 
20
- # Generates a JWT token the client (SPA) can pass to PostgREST for privilege escalation
21
- def generate_jwt
22
- payload = { role: self.lca_role }
23
- ::JWT.encode payload, LCA_OPTIONS[:jwt_secret], LCA_OPTIONS[:jwt_encryption]
24
- end # .generate_jwt
25
18
 
19
+ def lca_role
20
+ "active_tree_owner_#{id}_#{ ACTIVE_TREE_OPTIONS[:owner_role_suffix] }"
21
+ end # role
26
22
 
27
- # Returns the LCA table name as configured within config/lca.yml
28
- def lca_table_name
29
- LCA_OPTIONS[:table_name]
30
- end # .lca_table_name
31
23
 
24
+ def lca_table_name
25
+ ACTIVE_TREE_OPTIONS[:table_name]
26
+ end
32
27
 
33
- # Creates LCA table partition and role for owner
34
- def lca_create_storage
35
28
 
36
- # create data partition
37
- lca_sql "create table if not exists #{lca_table_name}_#{id} partition of #{lca_table_name} for values in (#{id})"
38
- # TODO create partition indexes
29
+ def lca_create_storage
30
+ # create snapshots partition
31
+ lca_sql "create table if not exists #{lca_table_name}_snapshots_#{id} partition of #{lca_table_name}_snapshots for values in (#{id})"
32
+
33
+ # if roles are enabled, grant privileges
34
+ if ACTIVE_TREE_OPTIONS[:create_postgrest_roles]
35
+ lca_sql "grant all privileges on #{lca_table_name}_snapshots_#{id} to #{ lca_role }"
36
+ end
37
+ end
39
38
 
40
- # create snapshots partition
41
- lca_sql "create table if not exists #{lca_table_name}_snapshots_#{id} partition of #{lca_table_name}_snapshots for values in (#{id})"
42
- # TODO create partition indexes
43
39
 
44
- if LCA_OPTIONS[:create_postgrest_roles]
45
- # drop role if it exists
46
- lca_sql "drop role if exists #{ lca_role }"
40
+ def lca_delete_storage
41
+ # remove privs
42
+ if ACTIVE_TREE_OPTIONS[:create_postgrest_roles]
43
+ lca_sql "REVOKE ALL PRIVILEGES ON #{lca_table_name}_snapshots_#{id} FROM #{ lca_role }"
44
+ end
47
45
 
48
- # create role
49
- lca_sql "create role #{ lca_role }"
46
+ if ACTIVE_TREE_OPTIONS[:destroy_partition_on_owner_destroy]
47
+ lca_sql "drop table if exists #{lca_table_name}_snapshots_#{id}"
48
+ else
49
+ lca_sql "alter table #{lca_table_name}_snapshots detach partition #{ lca_role }"
50
+ end
51
+
52
+ end
50
53
 
51
- # grant privs
52
- lca_sql "grant all privileges on #{lca_table_name}_#{id} to #{ lca_role }"
53
- lca_sql "grant all privileges on #{lca_table_name}_snapshots_#{id} to #{ lca_role }"
54
- end
55
-
56
- end # create_storage
57
-
58
-
59
- # Deletes or detaches the partition and removes the role for this owner
60
- def lca_delete_storage
61
-
62
- if LCA_OPTIONS[:create_postgrest_roles]
63
- # revoke privs
64
- lca_sql "REVOKE ALL PRIVILEGES ON #{lca_table_name}_#{id} FROM #{ lca_role }"
65
- lca_sql "REVOKE ALL PRIVILEGES ON #{lca_table_name}_snapshots_#{id} FROM #{ lca_role }"
66
-
67
- # delete role
68
- lca_sql "drop role #{ lca_role }"
69
- end
70
-
71
- if LCA_OPTIONS[:destroy_partition_on_owner_destroy]
72
- # delete partition
73
- lca_sql "drop table if exists #{lca_table_name}_#{id}"
74
- lca_sql "drop table if exists #{lca_table_name}_snapshots_#{id}"
75
- else
76
- # detach and forget about it
77
- lca_sql "alter table #{lca_table_name} detach partition #{ lca_role }"
78
- lca_sql "alter table #{lca_table_name}_snapshots detach partition #{ lca_role }"
79
- end
80
- end # delete_storage
81
-
82
- def lca_sql sql
83
- ActiveRecord::Base.connection.execute sql
84
- end # lca_sql
85
54
 
55
+ def lca_sql(q)
56
+ ActiveRecord::Base.connection.execute q
57
+ end
86
58
  end # ClassMethods
87
59
  end
88
60
  end
@@ -1,6 +1,7 @@
1
1
  class Lca::Cycle::Product < Lca::Cycle
2
2
  # price?
3
3
  # quantity?
4
+ # quantity unit?
4
5
  # life time?
5
6
  # life time unit?
6
7
  end
@@ -1,4 +1,5 @@
1
1
  class Lca::Cycle::Service < Lca::Cycle
2
2
  # duration?
3
+ # duration unit?
3
4
  # price?
4
5
  end
@@ -12,12 +12,7 @@ class Lca::Impact < Lca::Model
12
12
  before_update :snapshot!
13
13
 
14
14
 
15
- def snapshot!
16
- return ::Lca::ImpactSnapshot.create(
17
- self.attributes
18
- .deep_symbolize_keys
19
- .slice(:impact_amount, :impact_amount_unit, :impact_factor, :impact_precision, :owner_id, :owner_type)
20
- .merge( impact_id: id )
21
- )
15
+ def take_snapshot!
16
+ return ::Lca::ImpactSnapshot.take! self
22
17
  end # .snapshot!
23
18
  end
@@ -3,8 +3,8 @@ class Lca::ImpactSnapshot < ActiveRecord::Base
3
3
  self.primary_key = :id
4
4
 
5
5
  def self.table_name
6
- return "#{::LCA_OPTIONS[:table_name]}_snapshots" if defined? ::LCA_OPTIONS
7
- return "lca_models_snapshots"
6
+ return "#{::ACTIVE_TREE_OPTIONS[:table_name]}_snapshots" if defined? ::ACTIVE_TREE_OPTIONS
7
+ return "active_tree_snapshots"
8
8
  end
9
9
 
10
10
 
@@ -16,6 +16,8 @@ class Lca::ImpactSnapshot < ActiveRecord::Base
16
16
  validates_presence_of :impact_factor
17
17
  validates_presence_of :impact_precision
18
18
 
19
+
20
+
19
21
  # pull data from owner's partition
20
22
  def self.owned_by(owner_id)
21
23
  return self if !owner_id.is_a? Integer
@@ -41,4 +43,13 @@ class Lca::ImpactSnapshot < ActiveRecord::Base
41
43
  model_class
42
44
  end # owned_by
43
45
 
46
+
47
+ def self.take!(impact)
48
+ return create(
49
+ impact.attributes
50
+ .deep_symbolize_keys
51
+ .slice(:impact_amount, :impact_amount_previous, :impact_amount_unit, :impact_factor, :impact_precision, :owner_id, :owner_type)
52
+ .merge( impact_id: impact.id )
53
+ )
54
+ end # .take!
44
55
  end
@@ -1,78 +1,5 @@
1
- class Lca::Model < ActiveRecord::Base
1
+ class Lca::Model < ActiveTree::Model
2
2
 
3
- include Lca::Statusable
4
3
 
5
- self.primary_key = :id
6
-
7
- ltree :path
8
-
9
- def self.table_name
10
- return ::LCA_OPTIONS[:table_name] if defined? ::LCA_OPTIONS
11
- return "lca_models"
12
- end
13
-
14
- belongs_to :owner, polymorphic: :true, required: true
15
-
16
- scope :match_path, -> (some_path) { where("path ~ ?", "#{some_path}") }
17
-
18
- #has_one_attached :picture
19
- #has_rich_text :description
20
-
21
- validates_presence_of :name, allow_blank: false
22
- validates_presence_of :path, allow_blank: false
23
-
24
- has_many :metadata, class_name: "::Lca::Metadata", dependent: :destroy, as: :model
25
-
26
- before_validation :set_defaults
27
- def set_defaults
28
- self.path ||= name.delete(" ").gsub(/[^0-9a-z ]/i, '') if name
29
- self.path_slug = path.parameterize if path
30
- self.metadata_inline ||= {}
31
- end
32
-
33
-
34
- # Scoping by owner in order to select the partition
35
- #
36
- # @param owner_id [Integer] the partition owner
37
- def self.owned_by(owner_id)
38
- # if we're looking for anything else but an integer, revert to the base class
39
- return self if !owner_id.is_a? Integer
40
-
41
- partition_suffix = "_#{owner_id}"
42
-
43
- table = "#{ self.table_name }#{ partition_suffix }"
44
-
45
- ApplicationRecord.connection.schema_cache.clear!
46
- return self if !ApplicationRecord.connection.schema_cache.data_source_exists? table
47
-
48
- # duplicate the class
49
- model_class = Class.new self
50
- original_class_name = self.name
51
-
52
- # ...for this owner
53
- class_name = "#{name}#{partition_suffix}"
54
-
55
- # specify the table
56
- model_class.define_singleton_method(:table_name) do
57
- table
58
- end
59
-
60
- # specify the name
61
- model_class.define_singleton_method(:name) do
62
- class_name
63
- end
64
-
65
- model_class.define_singleton_method(:sti_name) do
66
- original_class_name
67
- end
68
-
69
- # override the STI name lmfao
70
- model_class.define_singleton_method(:find_sti_class) do |p|
71
- original_class_name.constantize
72
- end
73
-
74
- # proceed
75
- model_class
76
- end # .owned_by
77
4
 
78
5
  end
@@ -1,2 +1,6 @@
1
1
  class Lca::Process::Transport < Lca::Process
2
- end
2
+ # distance?
3
+ # distance unit?
4
+ # duration?
5
+ # duration unit?
6
+ end
@@ -3,4 +3,9 @@ class Lca::Stage < Lca::Model
3
3
  belongs_to :cycle, class_name: "::Lca::Cycle", foreign_key: :parent_entity_id, required: true
4
4
  has_many :processes, class_name: "::Lca::Process", foreign_key: :parent_entity_id
5
5
 
6
+ # duration?
7
+ # duration unit?
8
+
9
+ # quantity?
10
+ # quantity unit?
6
11
  end
@@ -1,8 +1,3 @@
1
- class Lca::Queries::Query
2
-
3
- def valid_input?(v)
4
- return false if [ [], "", "0", 0, nil, [""], [0], ["0"], [nil], "all" ].include?(v)
5
- return true
6
- end
1
+ class Lca::Queries::Query < ActiveTree::Query
7
2
 
8
3
  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.6"
4
+ VERSION = "0.3.1"
5
5
  end
data/lib/lca.rb CHANGED
@@ -1,14 +1,13 @@
1
1
  # frozen_string_literal: true
2
- require "active_support/all"
3
2
 
3
+ require "active_support/all"
4
4
  require "active_record"
5
-
6
5
  require "pg_ltree"
6
+ require "active_tree"
7
7
 
8
8
  require_relative "lca/version"
9
9
 
10
- require_relative "lca/models/concerns/statusable"
11
- require_relative "lca/models/metadata"
10
+ require_relative "lca/models/concerns/lcable"
12
11
  require_relative "lca/models/model"
13
12
  require_relative "lca/models/cycle"
14
13
  require_relative "lca/models/stage"
@@ -39,39 +38,12 @@ require_relative "lca/models/impact/ecosphere/flora"
39
38
  require_relative "lca/models/impact/human_health/cancer"
40
39
  require_relative "lca/models/impact/technosphere/resource_availability"
41
40
 
42
- require_relative "lca/models/concerns/lcable"
43
41
  require_relative "lca/active_record"
44
42
 
45
- # TODO add query objects and builders
46
- #require "lca/queries/"
47
- #require "lca/builders/"
48
43
 
49
44
  module Lca
50
45
  class Error < StandardError; end
51
46
 
52
- # Your code goes here...
53
-
54
- class << self
55
- attr_accessor :lca_models
56
- attr_accessor :options
57
- end
58
-
59
- self.lca_models = []
60
-
61
- def self.lca_options
62
- @options ||= begin
63
- path = Rails.root.join("config", "lca.yml").to_s
64
- if File.exist?(path)
65
- YAML.load(ERB.new(File.read(path)).result)
66
- else
67
- {
68
- table_name: "lca_models",
69
- jwt_secret: "",
70
- jwt_encryption: "HS256"
71
- }
72
- end
73
- end
74
- end
75
47
 
76
48
  def self.env
77
49
  @env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lca
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick @ Earthster
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-30 00:00:00.000000000 Z
11
+ date: 2021-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -25,33 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activestorage
28
+ name: active_tree
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '6.0'
33
+ version: 0.3.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '6.0'
41
- - !ruby/object:Gem::Dependency
42
- name: actiontext
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '6.0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '6.0'
40
+ version: 0.3.1
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: pg_ltree
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -97,14 +83,10 @@ files:
97
83
  - bin/console
98
84
  - bin/setup
99
85
  - lib/generators/lca/install_generator.rb
100
- - lib/generators/lca/templates/config.yml.tt
101
- - lib/generators/lca/templates/initializer.rb.tt
102
86
  - lib/generators/lca/templates/migration.rb.tt
103
87
  - lib/lca.rb
104
88
  - lib/lca/active_record.rb
105
- - lib/lca/builders/builder.rb
106
89
  - lib/lca/models/concerns/lcable.rb
107
- - lib/lca/models/concerns/statusable.rb
108
90
  - lib/lca/models/cycle.rb
109
91
  - lib/lca/models/cycle/activity.rb
110
92
  - lib/lca/models/cycle/product.rb
@@ -119,7 +101,6 @@ files:
119
101
  - lib/lca/models/impact/technosphere.rb
120
102
  - lib/lca/models/impact/technosphere/resource_availability.rb
121
103
  - lib/lca/models/impact_snapshot.rb
122
- - lib/lca/models/metadata.rb
123
104
  - lib/lca/models/model.rb
124
105
  - lib/lca/models/process.rb
125
106
  - lib/lca/models/process/raw_resource.rb
@@ -1,18 +0,0 @@
1
- # the main table will be called "lca_models"
2
- # partitions will be called "lca_models_X" where X is the owner object ID
3
- table_name: "lca_models"
4
-
5
- # create PG roles for postgrest: anon and owner-specific roles
6
- create_postgrest_roles: true
7
-
8
- # on owner removal, detach the partition but preserve the table and data, or destroy the partition and data
9
- # if you choose to detach and preserve, in order to avoid table name collisions, make sure owner IDs are not reused
10
- destroy_partition_on_owner_destroy: true
11
-
12
- # jwt secret required for postgrest role switching
13
- jwt_secret: "supersecret"
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"
@@ -1,17 +0,0 @@
1
- LCA_OPTIONS ||= begin
2
- path = Rails.root.join("config", "lca.yml").to_s
3
- if File.exist?(path)
4
- YAML.load( ERB.new(File.read(path)).result ).deep_symbolize_keys
5
- else
6
- {
7
- table_name: "lca_models",
8
- create_postgrest_roles: true,
9
- jwt_secret: "supersecret",
10
- jwt_encryption: "HS256",
11
- destroy_partition_on_owner_destroy: true,
12
- owner_role_suffix: "changeme"
13
- }
14
- end
15
- end.merge({
16
- database_user: Rails.application.config.database_configuration[ Rails.env ].deep_symbolize_keys[:username]
17
- })
@@ -1,3 +0,0 @@
1
- class Lca::Builder
2
-
3
- end
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
- module Lca::Statusable
3
-
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- scope :active, -> { where(status: 1) }
8
- scope :inactive, -> { where(status: 0) }
9
- alias_method :enabled?, :active?
10
- alias_method :enable!, :active!
11
- alias_method :on!, :active!
12
- alias_method :disabled?, :inactive?
13
- alias_method :disable!, :inactive!
14
- alias_method :off!, :inactive!
15
- alias_method :toggle?, :toggle_status!
16
-
17
- before_create :set_default_status
18
- end
19
-
20
- def set_default_status
21
- self.status ||= 1
22
- end
23
-
24
- def toggle_status!
25
- if active?
26
- inactive!
27
- else
28
- active!
29
- end
30
- end
31
-
32
- def status?
33
- [:inactive, :active][ status ]
34
- end
35
-
36
- def active?
37
- status == 1
38
- end
39
- def inactive?
40
- status == 0
41
- end
42
-
43
- def active!
44
- self.update(status: 1)
45
- end
46
- def inactive!
47
- self.update(status: 0)
48
- end
49
- end
@@ -1,12 +0,0 @@
1
- class Lca::Metadata < ActiveRecord::Base
2
-
3
- belongs_to :model, polymorphic: true, required: true
4
-
5
- def self.table_name
6
- return "#{ ::LCA_OPTIONS[:table_name] }_metadata" if defined? ::LCA_OPTIONS
7
- return "lca_models_metadata"
8
- end
9
-
10
- validates_presence_of :key
11
-
12
- end