Browse Source

Merge branch 'master' of ssh://git.yannweb.net:2222/cli/Registra-API into serialize

Lou 3 years ago
parent
commit
655e16e2f4

+ 2
- 0
Gemfile View File

@@ -34,6 +34,8 @@ group :development do
34 34
   gem 'listen', '~> 3.3'
35 35
   # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
36 36
   gem 'spring'
37
+  # Generate erd (UML) schema from rails models
38
+  gem 'rails-erd'
37 39
 end
38 40
 
39 41
 # Windows does not include zoneinfo files, so bundle the tzinfo-data gem

+ 10
- 0
Gemfile.lock View File

@@ -66,6 +66,7 @@ GEM
66 66
       msgpack (~> 1.0)
67 67
     builder (3.2.4)
68 68
     byebug (11.1.3)
69
+    choice (0.2.0)
69 70
     concurrent-ruby (1.1.8)
70 71
     crass (1.0.6)
71 72
     erubi (1.10.0)
@@ -119,6 +120,11 @@ GEM
119 120
     rails-dom-testing (2.0.3)
120 121
       activesupport (>= 4.2.0)
121 122
       nokogiri (>= 1.6)
123
+    rails-erd (1.6.1)
124
+      activerecord (>= 4.2)
125
+      activesupport (>= 4.2)
126
+      choice (~> 0.2.0)
127
+      ruby-graphviz (~> 1.2)
122 128
     rails-html-sanitizer (1.3.0)
123 129
       loofah (~> 2.3)
124 130
     railties (6.1.3.1)
@@ -131,6 +137,9 @@ GEM
131 137
     rb-fsevent (0.10.4)
132 138
     rb-inotify (0.10.1)
133 139
       ffi (~> 1.0)
140
+    rexml (3.2.5)
141
+    ruby-graphviz (1.2.5)
142
+      rexml
134 143
     spring (2.1.1)
135 144
     sprockets (4.0.2)
136 145
       concurrent-ruby (~> 1.0)
@@ -163,6 +172,7 @@ DEPENDENCIES
163 172
   listen (~> 3.3)
164 173
   puma (~> 5.0)
165 174
   rails (~> 6.1.3, >= 6.1.3.1)
175
+  rails-erd
166 176
   spring
167 177
   sqlite3 (~> 1.4)
168 178
   table_print (~> 1.5)

+ 4
- 5
app/controllers/api/v1/activities_controller.rb View File

@@ -1,7 +1,6 @@
1 1
 class Api::V1::ActivitiesController < ApplicationController
2 2
   before_action :set_activity, only: %i[show update destroy]
3 3
   before_action :check_login
4
-  before_action :check_owner, only: %i[update destroy]
5 4
 
6 5
   def index
7 6
     render json: Activity.all
@@ -14,6 +13,10 @@ class Api::V1::ActivitiesController < ApplicationController
14 13
   def create
15 14
     activity = current_user.created_activities.build(activity_params)
16 15
     if activity.save
16
+      JoinedUserActivity.create!(
17
+        user_id: current_user.id,
18
+        activity_id: activity.id
19
+      )
17 20
       render json: activity, status: :created
18 21
     else
19 22
       render json: { errors: activity.errors }, status: :unprocessable_entity
@@ -43,8 +46,4 @@ class Api::V1::ActivitiesController < ApplicationController
43 46
   def set_activity
44 47
     @activity = Activity.find(params[:id])
45 48
   end
46
-
47
-  def check_owner
48
-    head :forbidden unless @activity.author_id == current_user&.id
49
-  end
50 49
 end

+ 3
- 0
app/models/activity.rb View File

@@ -1,6 +1,9 @@
1 1
 class Activity < ApplicationRecord
2 2
   belongs_to :author, class_name: "User"
3 3
   
4
+  has_one :joined_user_activity
5
+  has_one :user, :through => :joined_user_activity
6
+
4 7
   validates :name, presence: true
5 8
   validates :author, presence: true
6 9
 end

+ 4
- 0
app/models/joined_user_activity.rb View File

@@ -0,0 +1,4 @@
1
+class JoinedUserActivity < ApplicationRecord
2
+  belongs_to :user
3
+  belongs_to :activity
4
+end

+ 4
- 0
app/models/user.rb View File

@@ -5,5 +5,9 @@ class User < ApplicationRecord
5 5
   validates :password_digest, presence: true
6 6
 
7 7
   has_many :created_activities, foreign_key: 'author_id', class_name: 'Activity', dependent: :destroy
8
+
9
+  has_many :joined_user_activities
10
+  has_many :activities, :through => :joined_user_activities
11
+
8 12
   has_secure_password
9 13
 end

+ 10
- 0
db/migrate/20210429184439_create_joined_user_activities.rb View File

@@ -0,0 +1,10 @@
1
+class CreateJoinedUserActivities < ActiveRecord::Migration[6.1]
2
+  def change
3
+    create_table :joined_user_activities do |t|
4
+      t.references :user
5
+      t.references :activity
6
+
7
+      t.timestamps
8
+    end
9
+  end
10
+end

+ 10
- 1
db/schema.rb View File

@@ -10,7 +10,7 @@
10 10
 #
11 11
 # It's strongly recommended that you check this file into your version control system.
12 12
 
13
-ActiveRecord::Schema.define(version: 2021_04_13_142821) do
13
+ActiveRecord::Schema.define(version: 2021_04_29_184439) do
14 14
 
15 15
   create_table "activities", force: :cascade do |t|
16 16
     t.string "name", null: false
@@ -23,6 +23,15 @@ ActiveRecord::Schema.define(version: 2021_04_13_142821) do
23 23
     t.index ["name"], name: "index_activities_on_name"
24 24
   end
25 25
 
26
+  create_table "joined_user_activities", force: :cascade do |t|
27
+    t.integer "user_id"
28
+    t.integer "activity_id"
29
+    t.datetime "created_at", precision: 6, null: false
30
+    t.datetime "updated_at", precision: 6, null: false
31
+    t.index ["activity_id"], name: "index_joined_user_activities_on_activity_id"
32
+    t.index ["user_id"], name: "index_joined_user_activities_on_user_id"
33
+  end
34
+
26 35
   create_table "users", force: :cascade do |t|
27 36
     t.string "email", null: false
28 37
     t.string "username", null: false

+ 10
- 2
db/seeds.rb View File

@@ -2,11 +2,13 @@ User.delete_all
2 2
 User.reset_pk_sequence
3 3
 Activity.delete_all
4 4
 Activity.reset_pk_sequence
5
+JoinedUserActivity.delete_all
6
+JoinedUserActivity.reset_pk_sequence
5 7
 
6 8
 10.times do |i|
7 9
   name = Faker::Name.first_name.downcase
8 10
   user = User.create! username: "#{name}", email: "#{name}@email.com", password: "azerty"
9
-  puts "Created user ##{i} - #{user.username}"
11
+  puts "Created USER ##{i} - #{user.username}"
10 12
   
11 13
   2.times do
12 14
     activity = Activity.create!(
@@ -15,6 +17,12 @@ Activity.reset_pk_sequence
15 17
       description: Faker::Company.catch_phrase,
16 18
       author_id: user.id
17 19
     )
18
-    puts "Created activity \"#{activity.name}\" for #{activity.client}"
20
+    puts "Created ACTIVITY \"#{activity.name}\" for #{activity.client}"
21
+
22
+    joined_user_activity = JoinedUserActivity.create!(
23
+      user_id: user.id,
24
+      activity_id: activity.id
25
+    )
26
+    puts "Created ASSOCIATION ##{joined_user_activity.id}"
19 27
   end
20 28
 end

BIN
erd.pdf View File


+ 1
- 18
test/controllers/api/v1/activities_controller_test.rb View File

@@ -68,15 +68,7 @@ class Api::V1::ActivitiesControllerTest < ActionDispatch::IntegrationTest
68 68
     as: :json
69 69
     assert_response :forbidden
70 70
   end
71
-
72
-  test "should forbid update activity - not owner or admin" do
73
-    patch api_v1_activity_url(@activity),
74
-    params: { activity: { name: "Updated name" } },
75
-    headers: { Authorization: JsonWebToken.encode(user_id: users(:two).id) },
76
-    as: :json
77
-    assert_response :forbidden
78
-  end
79
-
71
+  
80 72
   #DESTROY
81 73
   test "should destroy activity" do
82 74
     assert_difference "Activity.count", -1 do
@@ -93,13 +85,4 @@ class Api::V1::ActivitiesControllerTest < ActionDispatch::IntegrationTest
93 85
     end
94 86
     assert_response :forbidden
95 87
   end
96
-
97
-  test "should forbid destroy activity - not owner or admin" do
98
-    assert_no_difference('Activity.count') do
99
-    delete api_v1_activity_url(@activity),
100
-    headers: { Authorization: JsonWebToken.encode(user_id: users(:two).id) },
101
-    as: :json
102
-    end
103
-    assert_response :forbidden
104
-  end
105 88
 end

+ 11
- 0
test/fixtures/joined_user_activities.yml View File

@@ -0,0 +1,11 @@
1
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+# This model initially had no columns defined. If you add columns to the
4
+# model remove the '{}' from the fixture names and add the columns immediately
5
+# below each fixture, per the syntax in the comments below
6
+#
7
+one: {}
8
+# column: value
9
+#
10
+two: {}
11
+# column: value

+ 0
- 5
test/fixtures/users.yml View File

@@ -3,9 +3,4 @@
3 3
 one:
4 4
   email: one@one.com
5 5
   username: OneUsername
6
-  password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
7
-
8
-two:
9
-  email: two@two.com
10
-  username: TwoUsername
11 6
   password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>

+ 7
- 0
test/models/joined_user_activity_test.rb View File

@@ -0,0 +1,7 @@
1
+require "test_helper"
2
+
3
+class JoinedUserActivityTest < ActiveSupport::TestCase
4
+  # test "the truth" do
5
+  #   assert true
6
+  # end
7
+end

Loading…
Cancel
Save