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

+ 10
- 0
Gemfile.lock View File

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

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

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

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

1
 class Activity < ApplicationRecord
1
 class Activity < ApplicationRecord
2
   belongs_to :author, class_name: "User"
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
   validates :name, presence: true
7
   validates :name, presence: true
5
   validates :author, presence: true
8
   validates :author, presence: true
6
 end
9
 end

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

1
+class JoinedUserActivity < ApplicationRecord
2
+  belongs_to :user
3
+  belongs_to :activity
4
+end

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

5
   validates :password_digest, presence: true
5
   validates :password_digest, presence: true
6
 
6
 
7
   has_many :created_activities, foreign_key: 'author_id', class_name: 'Activity', dependent: :destroy
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
   has_secure_password
12
   has_secure_password
9
 end
13
 end

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

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
 #
10
 #
11
 # It's strongly recommended that you check this file into your version control system.
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
   create_table "activities", force: :cascade do |t|
15
   create_table "activities", force: :cascade do |t|
16
     t.string "name", null: false
16
     t.string "name", null: false
23
     t.index ["name"], name: "index_activities_on_name"
23
     t.index ["name"], name: "index_activities_on_name"
24
   end
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
   create_table "users", force: :cascade do |t|
35
   create_table "users", force: :cascade do |t|
27
     t.string "email", null: false
36
     t.string "email", null: false
28
     t.string "username", null: false
37
     t.string "username", null: false

+ 10
- 2
db/seeds.rb View File

2
 User.reset_pk_sequence
2
 User.reset_pk_sequence
3
 Activity.delete_all
3
 Activity.delete_all
4
 Activity.reset_pk_sequence
4
 Activity.reset_pk_sequence
5
+JoinedUserActivity.delete_all
6
+JoinedUserActivity.reset_pk_sequence
5
 
7
 
6
 10.times do |i|
8
 10.times do |i|
7
   name = Faker::Name.first_name.downcase
9
   name = Faker::Name.first_name.downcase
8
   user = User.create! username: "#{name}", email: "#{name}@email.com", password: "azerty"
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
   2.times do
13
   2.times do
12
     activity = Activity.create!(
14
     activity = Activity.create!(
15
       description: Faker::Company.catch_phrase,
17
       description: Faker::Company.catch_phrase,
16
       author_id: user.id
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
   end
27
   end
20
 end
28
 end

BIN
erd.pdf View File


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

68
     as: :json
68
     as: :json
69
     assert_response :forbidden
69
     assert_response :forbidden
70
   end
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
   #DESTROY
72
   #DESTROY
81
   test "should destroy activity" do
73
   test "should destroy activity" do
82
     assert_difference "Activity.count", -1 do
74
     assert_difference "Activity.count", -1 do
93
     end
85
     end
94
     assert_response :forbidden
86
     assert_response :forbidden
95
   end
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
 end
88
 end

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

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

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

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