soc_med 0.1.1 → 0.1.2

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: b9e72d4977e041a7094a05d9159bdc904a2d962f69cbf4488c8e1deb3affca9a
4
- data.tar.gz: 69fa2e2a15ccab49f551997505d5613d971227bea7fe78d9f025d86e9d9b2902
3
+ metadata.gz: 98f79c6e70f6bc5ae4ce4eeb641ffb73221ea3172776aad6615b4879faf8610a
4
+ data.tar.gz: 1d9955bd3377f1c3528c8340997eff4e471b47132c502261c5330e1d334f8a9b
5
5
  SHA512:
6
- metadata.gz: 46b00d2074041e967eedb6cd6fdf21ffe1c98bac9f7bcf2a95c53aa623445daf32bfcf2000a8b678969e07fbaf4ef1acde5f36273e82ab675b4ae1d72a0225c9
7
- data.tar.gz: eab8bf92c6315a2e6a73c75e98482158f8b4b18be80c4387ab5678f885f39b6f8fb777af8cf0bb070743caa476c6fa2c1ce143ab83b0ef6d4313a05e5b6d511b
6
+ metadata.gz: 68511dd6c417e409f4f9df4907665bfd1229868ff480f023feb2323f775c0831c2c143be2ce38f5ccdb96a7bbda7bbe5a5de80f4df5321cd54ac64dd7dac172b
7
+ data.tar.gz: c318a996dbf469bcca73accd9cc9e5b5482b1317cf27068822a1dafaa4c6403e797034951de918e04f27d12c2f16922049b6b50dd9b72c8f4a3dce2bcdbadd9a
@@ -3,22 +3,9 @@ require 'dry_serialization/blueprinter'
3
3
  module SocMed
4
4
  class ApplicationController < ActionController::Base
5
5
  include DrySerialization::Blueprinter
6
- protect_from_forgery unless: -> { request.format.json? }
7
-
8
- def error_response(error)
9
- default_response = default_response(error)
10
- render json: default_response
11
- end
6
+ include DrySerialization::SerializationHelper
12
7
 
13
- def success_response(data, message = nil)
14
- default_response = default_response(message, data)
15
- render json: default_response
16
- end
17
-
18
- private
8
+ protect_from_forgery unless: -> { request.format.json? }
19
9
 
20
- def default_response(message, data = {})
21
- { message: message }.merge(data)
22
- end
23
10
  end
24
11
  end
@@ -1,16 +1,6 @@
1
1
  module SocMed
2
2
  class Block < ApplicationRecord
3
- belongs_to :target, polymorphic: true
4
- belongs_to :owner, polymorphic: true
3
+ include Concerns::SocMedHelper
5
4
 
6
- before_validation :raise_already_liked_error_if_required, on: :create
7
-
8
- validates :target_id, uniqueness: {scope: %i[target_type owner_id owner_type], message: "has already been blocked."}
9
-
10
- private
11
-
12
- def raise_already_followd_error_if_required
13
- raise SocMed::Follows::AlreadyExistsError.new("#{owner_type} has already blocked that #{target_type}") if owner.blockable_objects.exists?(target: target)
14
- end
15
5
  end
16
6
  end
@@ -1,9 +1,29 @@
1
1
  module SocMed
2
2
  module Concerns
3
3
  module SocMedHelper
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ belongs_to :target, polymorphic: true
8
+ belongs_to :owner, polymorphic: true
9
+
10
+ before_validation :raise_already_implemented_error_if_required, on: :create
11
+ before_validation :raise_not_implemented_error, on: :create
12
+
13
+ validates :target_id, uniqueness: {scope: %i[target_type owner_id owner_type], message: "has already been #{self.class.to_s.split('/')[-1]}ed."}
14
+ end
4
15
 
5
16
  def raise_not_implemented_error
6
- raise NotImplementedError unless target.respond_to?("number_of_#{self.class.to_s.underscore.split('/')[-1].pluralize}")
17
+ unless target.respond_to?("number_of_#{self.class.to_s.underscore.split('/')[-1].pluralize}")
18
+ raise NotImplementedError
19
+ end
20
+ end
21
+
22
+ def raise_already_implemented_error_if_required
23
+ klass = self.class.to_s.underscore.split('/').last
24
+ if owner.send("#{klass}able_objects").exists?(target: target)
25
+ raise "SocMed::#{klass.plurlarize.titleize}::AlreadyExistsError" "#{owner_type} has already liked that #{target_type}"
26
+ end
7
27
  end
8
28
 
9
29
  def update_count(attribute, operator)
@@ -1,19 +1,10 @@
1
1
  module SocMed
2
2
  class Follow < ApplicationRecord
3
- include SocMed::SocMedHelper
3
+ include Concerns::SocMedHelper
4
4
 
5
- belongs_to :target, polymorphic: true
6
- belongs_to :owner, polymorphic: true
7
-
8
-
9
- before_validation :raise_already_liked_error_if_required, on: :create
10
- before_validation :raise_not_implemented_error, on: :create
11
5
  before_commit :increment_number_of_follows, on: :create
12
6
  before_destroy :decrement_number_of_follows
13
7
 
14
- validates :target_id, uniqueness: {scope: %i[target_type owner_id owner_type], message: "has already been followed."}
15
-
16
-
17
8
  private
18
9
 
19
10
  def increment_number_of_follows
@@ -28,8 +19,5 @@ module SocMed
28
19
  update_count(:number_of_follows, :-)
29
20
  end
30
21
 
31
- def raise_already_followd_error_if_required
32
- raise SocMed::Follows::AlreadyExistsError.new("#{owner_type} has already followed that #{target_type}") if owner.followable_objects.exists?(target: target)
33
- end
34
22
  end
35
23
  end
@@ -1,20 +1,10 @@
1
- require_relative './concerns/soc_med_helper'
2
-
3
1
  module SocMed
4
2
  class Like < ::ApplicationRecord
5
3
  include Concerns::SocMedHelper
6
4
 
7
- belongs_to :target, polymorphic: true
8
- belongs_to :owner, polymorphic: true
9
-
10
- before_validation :raise_already_liked_error_if_required, on: :create
11
- before_validation :raise_not_implemented_error, on: :create
12
5
  before_commit :increment_number_of_likes, on: :create
13
6
  before_destroy :decrement_number_of_likes
14
7
 
15
- validates :target_id, uniqueness: {scope: %i[target_type owner_id owner_type], message: "has already been liked."}
16
-
17
-
18
8
  private
19
9
 
20
10
  def increment_number_of_likes
@@ -29,10 +19,5 @@ module SocMed
29
19
  update_count(:number_of_likes, :-)
30
20
  end
31
21
 
32
- private
33
-
34
- def raise_already_liked_error_if_required
35
- raise SocMed::Likes::AlreadyExistsError.new("#{owner_type} has already liked that #{target_type}") if owner.likeable_objects.exists?(target: target)
36
- end
37
22
  end
38
23
  end
@@ -1,16 +1,23 @@
1
1
  module SocMed
2
2
  class Report < ApplicationRecord
3
- belongs_to :target, polymorphic: true
4
- belongs_to :owner, polymorphic: true
3
+ include Concerns::SocMedHelper
5
4
 
6
- before_validation :raise_already_liked_error_if_required, on: :create
7
-
8
- validates :target_id, uniqueness: {scope: %i[target_type owner_id owner_type], message: "has already been reported."}
5
+ before_commit :increment_number_of_reports, on: :create
6
+ before_destroy :decrement_number_of_reports
9
7
 
10
8
  private
11
9
 
12
- def raise_already_followd_error_if_required
13
- raise SocMed::Reports::AlreadyExistsError.new("#{owner_type} has already reported that #{target_type}") if owner.reportable_objects.exists?(target: target)
10
+ def increment_number_of_reports
11
+ raise_not_implemented_error
12
+
13
+ update_count(:number_of_reports, :+)
14
+ end
15
+
16
+ def decrement_number_of_reports
17
+ raise_not_implemented_error
18
+
19
+ update_count(:number_of_reports, :-)
14
20
  end
15
21
  end
22
+
16
23
  end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators/base'
2
+
3
+ module SocMed
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../../templates', __FILE__)
6
+
7
+ def self.exit_on_failure
8
+ true
9
+ end
10
+
11
+ def install_soc_med_migrations
12
+ say 'Copying migrations...'
13
+ run 'rails soc_med:install:migrations'
14
+ end
15
+
16
+ def install_soc_med_initializer
17
+ say 'Creating intializer...'
18
+ template 'soc_med.rb', 'config/initializers/soc_med.rb'
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,4 @@
1
+ SocMed.configure do |config|
2
+ # Owner Class defaults to user. Change it here to whatever the owner class is in your application.
3
+ # config.owner_class = :user
4
+ end
@@ -11,11 +11,17 @@ require 'soc_med/services/reports/create'
11
11
  require 'soc_med/services/reports/destroy'
12
12
  require 'soc_med/services/blocks/create'
13
13
  require 'soc_med/services/blocks/destroy'
14
+ require 'soc_med/configuration'
14
15
 
15
16
  module SocMed
16
- mattr_accessor :owner_class
17
17
 
18
- def self.owner_class
19
- @@owner_class.to_s.classify.constantize
18
+ class << self
19
+ attr_accessor :configuration
20
20
  end
21
+
22
+ def self.configure
23
+ self.configuration || Configuration.new
24
+ yield(configuration)
25
+ end
26
+
21
27
  end
@@ -13,8 +13,8 @@ module SocMed
13
13
  attributes = [attributes] unless attributes.is_a?(Array)
14
14
 
15
15
  attributes.each do |attribute|
16
- class_eval "has_many :by_blocking_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Block', source: :owner, source_type: '#{attribute.to_s.classify}', foreign_key: :owner_id, dependent: :destroy"
17
- class_eval "has_many :blocked_#{attribute}, through: :by_blocking_#{attribute}, source: :target, source_type: '#{attribute.to_s.classify}'"
16
+ class_eval "has_many :by_blocking_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Block', foreign_key: :owner_id, dependent: :destroy"
17
+ class_eval "has_many :blocked_#{attribute}, through: :by_blocking_#{attribute}, class_name: '#{attribute.to_s.classify}'"
18
18
  end
19
19
  end
20
20
 
@@ -22,8 +22,8 @@ module SocMed
22
22
  attributes = [attributes] unless attributes.is_a?(Array)
23
23
 
24
24
  attributes.each do |attribute|
25
- class_eval "has_many :by_blocked_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Block', source: :target, source_type: '#{attribute.to_s.classify}', foreign_key: :target_id, dependent: :destroy"
26
- class_eval "has_many :blocked_by_#{attribute}, through: :by_blocked_#{attribute}, source: :owner, source_type: '#{attribute.to_s.classify}'"
25
+ class_eval "has_many :by_blocked_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Block', foreign_key: :target_id, dependent: :destroy"
26
+ class_eval "has_many :blocked_by_#{attribute}, through: :by_blocked_#{attribute}, class_name: '#{attribute.to_s.classify}'"
27
27
  end
28
28
  end
29
29
 
@@ -13,8 +13,8 @@ module SocMed
13
13
  attributes = [attributes] unless attributes.is_a?(Array)
14
14
 
15
15
  attributes.each do |attribute|
16
- class_eval "has_many :by_following_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Follow', source: :owner, source_type: '#{attribute.to_s.classify}', foreign_key: :owner_id, dependent: :destroy"
17
- class_eval "has_many :followed_#{attribute}, through: :by_following_#{attribute}, source: :target, source_type: '#{attribute.to_s.classify}'"
16
+ class_eval "has_many :by_following_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Follow', foreign_key: :owner_id, dependent: :destroy"
17
+ class_eval "has_many :followed_#{attribute}, through: :by_following_#{attribute}, class_name: '#{attribute.to_s.classify}'"
18
18
  end
19
19
  end
20
20
 
@@ -22,8 +22,8 @@ module SocMed
22
22
  attributes = [attributes] unless attributes.is_a?(Array)
23
23
 
24
24
  attributes.each do |attribute|
25
- class_eval "has_many :by_followed_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Follow', source: :target, source_type: '#{attribute.to_s.classify}', foreign_key: :target_id, dependent: :destroy"
26
- class_eval "has_many :followed_by_#{attribute}, through: :by_followed_#{attribute}, source: :owner, source_type: '#{attribute.to_s.classify}'"
25
+ class_eval "has_many :by_followed_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Follow', foreign_key: :target_id, dependent: :destroy"
26
+ class_eval "has_many :followed_by_#{attribute}, through: :by_followed_#{attribute}, class_name: '#{attribute.to_s.classify}'"
27
27
  end
28
28
  end
29
29
 
@@ -13,8 +13,8 @@ module SocMed
13
13
  attributes = [attributes] unless attributes.is_a?(Array)
14
14
 
15
15
  attributes.each do |attribute|
16
- class_eval "has_many :by_liking_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Like', source: :owner, source_type: '#{attribute.to_s.classify}', foreign_key: :owner_id, dependent: :destroy"
17
- class_eval "has_many :liked_#{attribute}, through: :by_liking_#{attribute}, source: :target, source_type: '#{attribute.to_s.classify}'"
16
+ class_eval "has_many :by_liking_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Like', foreign_key: :owner_id, dependent: :destroy"
17
+ class_eval "has_many :liked_#{attribute}, through: :by_liking_#{attribute}, class_name: '#{attribute.to_s.classify}'"
18
18
  end
19
19
  end
20
20
 
@@ -22,8 +22,8 @@ module SocMed
22
22
  attributes = [attributes] unless attributes.is_a?(Array)
23
23
 
24
24
  attributes.each do |attribute|
25
- class_eval "has_many :by_liked_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Like', source: :target, source_type: '#{attribute.to_s.classify}', foreign_key: :target_id, dependent: :destroy"
26
- class_eval "has_many :liked_by_#{attribute}, through: :by_liked_#{attribute}, source: :owner, source_type: '#{attribute.to_s.classify}'"
25
+ class_eval "has_many :by_liked_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Like', foreign_key: :target_id, dependent: :destroy"
26
+ class_eval "has_many :liked_by_#{attribute}, through: :by_liked_#{attribute}, class_name: '#{attribute.to_s.classify}'"
27
27
  end
28
28
  end
29
29
 
@@ -13,8 +13,8 @@ module SocMed
13
13
  attributes = [attributes] unless attributes.is_a?(Array)
14
14
 
15
15
  attributes.each do |attribute|
16
- class_eval "has_many :by_reporting_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Report', source: :owner, source_type: '#{attribute.to_s.classify}', foreign_key: :owner_id, dependent: :destroy"
17
- class_eval "has_many :reported_#{attribute}, through: :by_reporting_#{attribute}, source: :target, source_type: '#{attribute.to_s.classify}'"
16
+ class_eval "has_many :by_reporting_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Report', foreign_key: :owner_id, dependent: :destroy"
17
+ class_eval "has_many :reported_#{attribute}, through: :by_reporting_#{attribute}, class_name: '#{attribute.to_s.classify}'"
18
18
  end
19
19
  end
20
20
 
@@ -22,8 +22,8 @@ module SocMed
22
22
  attributes = [attributes] unless attributes.is_a?(Array)
23
23
 
24
24
  attributes.each do |attribute|
25
- class_eval "has_many :by_reported_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Report', source: :target, source_type: '#{attribute.to_s.classify}', foreign_key: :target_id, dependent: :destroy"
26
- class_eval "has_many :reported_by_#{attribute}, through: :by_reported_#{attribute}, source: :owner, source_type: '#{attribute.to_s.classify}'"
25
+ class_eval "has_many :by_reported_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Report', foreign_key: :target_id, dependent: :destroy"
26
+ class_eval "has_many :reported_by_#{attribute}, through: :by_reported_#{attribute}, class_name: '#{attribute.to_s.classify}'"
27
27
  end
28
28
  end
29
29
  end
@@ -0,0 +1,10 @@
1
+ module SocMed
2
+ class Configuration
3
+ attr_accessor :owner_class
4
+
5
+ def initialize
6
+ @owner_class = :user
7
+ end
8
+
9
+ end
10
+ end
@@ -2,6 +2,7 @@ require 'active_record'
2
2
  require 'soc_med/services/no_trigger'
3
3
  require 'soc_med/services/success'
4
4
  require 'soc_med/services/failure'
5
+ require 'soc_med/configuration'
5
6
 
6
7
  module SocMed
7
8
  module Services
@@ -9,7 +10,7 @@ module SocMed
9
10
  attr_reader :params
10
11
 
11
12
  def self.call(params, &block)
12
- new( params).call(&block)
13
+ new(params).call(&block)
13
14
  end
14
15
 
15
16
  def initialize(params)
@@ -24,6 +25,19 @@ module SocMed
24
25
 
25
26
  private
26
27
 
28
+ def owner_class
29
+ @owner_class ||= Configuration.new.owner_class.to_s.classify.constantize
30
+ end
31
+
32
+ def handle_error(error)
33
+ unless Rails.env.test?
34
+ Rails.logger.error(error.message)
35
+ puts error.backtrace.join("\n\t")
36
+ end
37
+
38
+ yield(NoTrigger, Failure.new(error.message))
39
+ end
40
+
27
41
  def target
28
42
  raise NotImplementedError
29
43
  end
@@ -11,6 +11,10 @@ module SocMed
11
11
  yield(error)
12
12
  end
13
13
 
14
+ def success?
15
+ # Kept nil to return false
16
+ end
17
+
14
18
  end
15
19
  end
16
20
  end
@@ -10,7 +10,7 @@ module SocMed
10
10
 
11
11
  yield(Success.new(like), NoTrigger)
12
12
  rescue SocMed::Likes::AlreadyExistsError, StandardError => e
13
- yield(NoTrigger, Failure.new(e))
13
+ handle_error(e)
14
14
  end
15
15
 
16
16
  private
@@ -28,8 +28,8 @@ module SocMed
28
28
  end
29
29
 
30
30
  def owner
31
- owner_type, owner_id = owner_info
32
- owner = SocMed.owner_class.find_by(id: owner_id)
31
+ _owner_type, owner_id = owner_info
32
+ owner = owner_class.find_by(id: owner_id)
33
33
  raise ActiveRecord::RecordNotFound unless owner
34
34
 
35
35
  owner
@@ -11,6 +11,10 @@ module SocMed
11
11
  yield(object)
12
12
  end
13
13
 
14
+ def success?
15
+ true
16
+ end
17
+
14
18
  end
15
19
  end
16
20
  end
@@ -1,3 +1,3 @@
1
1
  module SocMed
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soc_med
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Heft
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-01 00:00:00.000000000 Z
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 6.0.3
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 6.0.3.1
19
+ version: '0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: 6.0.3
30
24
  - - ">="
31
25
  - !ruby/object:Gem::Version
32
- version: 6.0.3.1
26
+ version: '0'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: dry_serialization
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,6 +38,20 @@ dependencies:
44
38
  - - ">="
45
39
  - !ruby/object:Gem::Version
46
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jsonapi-serializer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: blueprinter
49
57
  requirement: !ruby/object:Gem::Requirement
@@ -128,11 +136,14 @@ files:
128
136
  - db/migrate/20200605151423_create_soc_med_follows.rb
129
137
  - db/migrate/20200605151437_create_soc_med_blocks.rb
130
138
  - db/migrate/20200605151450_create_soc_med_reports.rb
139
+ - lib/generators/soc_med/install_generator.rb
140
+ - lib/generators/templates/soc_med.rb
131
141
  - lib/soc_med.rb
132
142
  - lib/soc_med/concerns/blockable.rb
133
143
  - lib/soc_med/concerns/followable.rb
134
144
  - lib/soc_med/concerns/likeable.rb
135
145
  - lib/soc_med/concerns/reportable.rb
146
+ - lib/soc_med/configuration.rb
136
147
  - lib/soc_med/engine.rb
137
148
  - lib/soc_med/services/base_service.rb
138
149
  - lib/soc_med/services/blocks/block_base.rb
@@ -173,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
184
  - !ruby/object:Gem::Version
174
185
  version: '0'
175
186
  requirements: []
176
- rubygems_version: 3.0.8
187
+ rubygems_version: 3.2.3
177
188
  signing_key:
178
189
  specification_version: 4
179
190
  summary: Provides social media functionality to Rails applications