Module: FinePrint

Defined in:
lib/fine_print.rb,
lib/fine_print/engine.rb,
lib/fine_print/version.rb,
lib/fine_print/configuration.rb,
app/models/fine_print/contract.rb,
app/models/fine_print/signature.rb,
lib/fine_print/action_controller/base.rb,
app/helpers/fine_print/application_helper.rb,
app/controllers/fine_print/home_controller.rb,
app/controllers/fine_print/contracts_controller.rb,
app/controllers/fine_print/signatures_controller.rb,
app/controllers/fine_print/application_controller.rb

Defined Under Namespace

Modules: ActionController, ApplicationHelper Classes: ApplicationController, Configuration, Contract, ContractsController, Engine, HomeController, Signature, SignaturesController

Constant Summary collapse

SIGNATURE_IS_IMPLICIT =
true
SIGNATURE_IS_EXPLICIT =
false
VERSION =
'6.0.1'

Class Method Summary collapse

Class Method Details

.configObject



9
10
11
# File 'lib/fine_print.rb', line 9

def self.config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



13
14
15
# File 'lib/fine_print.rb', line 13

def self.configure
  yield config
end

.get_contract(reference) ⇒ Object

Gets a contract, given either the contract object, ID or name If given a name, returns the latest published version of that contract

- contract - can be a Contract object, its ID, or its name

Raises:

  • (ActiveRecord::RecordNotFound)


20
21
22
23
24
25
26
27
# File 'lib/fine_print.rb', line 20

def self.get_contract(reference)
  return reference if reference.is_a? Contract
  num = Integer(reference) rescue nil
  return Contract.find(num) if num
  contract = Contract.where(name: reference.to_s).published.first
  return contract if contract
  raise ActiveRecord::RecordNotFound, "Couldn't find Contract with 'name'=#{reference.to_s}"
end

.latest_published_contracts(conditions = {}) ⇒ Object

Returns all the latest published contracts that match the given conditions.



80
81
82
# File 'lib/fine_print.rb', line 80

def self.latest_published_contracts(conditions = {})
  Contract.published.latest.where(conditions)
end

.sign_contract(user, contract, is_implicit = SIGNATURE_IS_EXPLICIT, max_attempts = 3) ⇒ Object

Records that the given user has signed the given contract

- user - the user in question
- contract - can be a Contract object, its ID, or its name (String/Symbol)
- is_implicit - if true, the signature is implicit/assumed/indirectly-acquired
                if false, the signature was obtained directly from the user


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fine_print.rb', line 34

def self.sign_contract(user, contract, is_implicit = SIGNATURE_IS_EXPLICIT, max_attempts = 3)
  attempts = 0

  begin
    Signature.transaction(requires_new: true) do
      contract = get_contract(contract)

      Signature.create do |signature|
        signature.user = user
        signature.contract = contract
        signature.is_implicit = is_implicit
      end
    end
  rescue ActiveRecord::RecordNotUnique => exception
    attempts += 1
    raise exception if attempts >= max_attempts

    # Simply retry as in https://apidock.com/rails/v4.0.2/ActiveRecord/Relation/find_or_create_by
    # If it already exists, the validations should catch it this time
    retry
  end
end

.signed_any_version_of_contract?(user, contract) ⇒ Boolean

Returns true iff the given user has signed any version of the given contract

- user - the user in question
- contract - can be a Contract object, its ID, or its name (String/Symbol)

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/fine_print.rb', line 71

def self.signed_any_version_of_contract?(user, contract)
  return false if user.nil?

  contract = get_contract(contract)

  contract.same_name.signed_by(user).exists?
end

.signed_contract?(user, contract) ⇒ Boolean

Returns true iff the given user has signed the given contract

- user - the user in question
- contract - can be a Contract object, its ID, or its name (String/Symbol)

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/fine_print.rb', line 60

def self.signed_contract?(user, contract)
  return false if user.nil?

  contract = get_contract(contract)

  contract.signed_by?(user)
end

.signed_contracts_for(user, conditions = {}) ⇒ Object

Returns all contracts matching the given conditions whose latest published version the user has signed.

- user - the user in question
- conditions - filters the list of contracts to check

If no conditions are provided, all latest contracts are checked.



89
90
91
92
93
# File 'lib/fine_print.rb', line 89

def self.signed_contracts_for(user, conditions = {})
  return [] if user.nil?

  latest_published_contracts(conditions).signed_by(user)
end

.unsigned_contracts_for(user, conditions = {}) ⇒ Object

Returns all contracts matching the given conditions whose latest published version the user has not signed.

- user - the user in question
- conditions - filters the list of contracts to check

If no conditions are provided, all latest contracts are checked.



100
101
102
103
104
# File 'lib/fine_print.rb', line 100

def self.unsigned_contracts_for(user, conditions = {})
  contracts = latest_published_contracts(conditions)
  signed_contracts = signed_contracts_for(user, conditions)
  contracts - signed_contracts
end