• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MushroomObserver / mushroom-observer / 13218375653

08 Feb 2025 06:48PM UTC coverage: 93.38% (+0.006%) from 93.374%
13218375653

push

github

mo-nathan
Replace fake Lentinellus ursinus example with legit case of differing authors

27380 of 29321 relevant lines covered (93.38%)

581.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.24
/app/classes/api2/location_api.rb
1
# frozen_string_literal: true
2

3
class API2
1✔
4
  # API for Location
5
  class LocationAPI < ModelAPI
1✔
6
    def model
1✔
7
      Location
65✔
8
    end
9

10
    def high_detail_page_length
1✔
11
      100
6✔
12
    end
13

14
    def low_detail_page_length
1✔
15
      1000
4✔
16
    end
17

18
    def put_page_length
1✔
19
      1000
×
20
    end
21

22
    def delete_page_length
1✔
23
      1000
×
24
    end
25

26
    def high_detail_includes
1✔
27
      [
28
        { comments: :user }
3✔
29
      ]
30
    end
31

32
    def query_params
1✔
33
      box = parse_bounding_box!
29✔
34
      {
35
        where: sql_id_condition,
25✔
36
        created_at: parse_range(:time, :created_at),
37
        updated_at: parse_range(:time, :updated_at),
38
        users: parse_array(:user, :user, help: :first_user),
39
        in_box: box
40
      }
41
    end
42

43
    def create_params
1✔
44
      {
45
        display_name: parse(:string, :name, limit: 1024, help: "postal"),
13✔
46
        north: parse(:latitude, :north),
47
        south: parse(:longitude, :south),
48
        east: parse(:longitude, :east),
49
        west: parse(:longitude, :west),
50
        high: parse(:altitude, :high),
51
        low: parse(:altitude, :low),
52
        notes: parse(:string, :notes),
53
        user: @user
54
      }
55
    end
56

57
    def update_params
1✔
58
      {
59
        display_name: parse(:string, :set_name, limit: 1024, not_blank: true,
11✔
60
                                                help: "postal"),
61
        north: parse(:latitude, :set_north),
62
        south: parse(:longitude, :set_south),
63
        east: parse(:longitude, :set_east),
64
        west: parse(:longitude, :set_west),
65
        high: parse(:altitude, :set_high),
66
        low: parse(:altitude, :set_low),
67
        notes: parse(:string, :set_notes)
68
      }
69
    end
70

71
    def validate_create_params!(params)
1✔
72
      name = params[:display_name]
12✔
73
      raise(MissingParameter.new(:name))  unless params[:display_name]
12✔
74
      raise(MissingParameter.new(:north)) unless params[:north]
11✔
75
      raise(MissingParameter.new(:south)) unless params[:south]
9✔
76
      raise(MissingParameter.new(:east))  unless params[:east]
8✔
77
      raise(MissingParameter.new(:west))  unless params[:west]
7✔
78

79
      make_sure_location_doesnt_exist!(name)
6✔
80
      make_sure_location_isnt_dubious!(name)
5✔
81
    end
82

83
    def validate_update_params!(params)
1✔
84
      name = params[:display_name]
8✔
85
      make_sure_location_doesnt_exist!(name)
8✔
86
      make_sure_location_isnt_dubious!(name)
7✔
87
      make_sure_not_setting_name_of_multiple_locations!
6✔
88
      raise(MissingSetParameters.new) if params.empty?
6✔
89
    end
90

91
    def delete
1✔
92
      raise(NoMethodForAction.new("DELETE", action))
2✔
93
    end
94

95
    # Our restrictions on edit permissions for the API are much more strict
96
    # than on the website.  Revoke permission if anyone other than the creator
97
    # owns any attached objects: location versions, descriptions, observations,
98
    # species lists, users (i.e. profile location), or herbaria.
99
    def must_have_edit_permission!(loc)
1✔
100
      must_be_creator!(loc)
6✔
101
      must_be_only_editor!(loc)
5✔
102
      must_own_all_descriptions!(loc)
5✔
103
      must_own_all_observations!(loc)
5✔
104
      must_own_all_species_lists!(loc)
4✔
105
      must_not_be_another_users_profile_location!(loc)
3✔
106
      must_not_have_any_herbaria!(loc)
2✔
107
    end
108

109
    ############################################################################
110

111
    private
1✔
112

113
    def must_be_creator!(loc)
1✔
114
      return if loc.user == @user
6✔
115

116
      raise(MustBeCreator.new(:location))
1✔
117
    end
118

119
    def must_be_only_editor!(loc)
1✔
120
      return unless loc.versions.any? { |x| x.user_id != @user.id }
15✔
121

122
      raise(MustBeOnlyEditor.new(:location))
×
123
    end
124

125
    def must_own_all_descriptions!(loc)
1✔
126
      return unless loc.descriptions.any? { |x| x.user != @user }
10✔
127

128
      raise(MustOwnAllDescriptions.new(:location))
×
129
    end
130

131
    def must_own_all_observations!(loc)
1✔
132
      return unless loc.observations.any? { |x| x.user != @user }
10✔
133

134
      raise(MustOwnAllObservations.new(:location))
1✔
135
    end
136

137
    def must_own_all_species_lists!(loc)
1✔
138
      return unless loc.species_lists.any? { |x| x.user != @user }
8✔
139

140
      raise(MustOwnAllSpeciesLists.new(:location))
1✔
141
    end
142

143
    def must_not_be_another_users_profile_location!(loc)
1✔
144
      return unless loc.users.any? { |x| x != @user }
6✔
145

146
      raise(AnotherUsersProfileLocation.new)
1✔
147
    end
148

149
    def must_not_have_any_herbaria!(loc)
1✔
150
      return unless loc.herbaria.any?
2✔
151

152
      raise(MustNotHaveAnyHerbaria.new)
1✔
153
    end
154

155
    def make_sure_location_doesnt_exist!(name)
1✔
156
      return unless Location.find_by_name_or_reverse_name(name)
14✔
157

158
      raise(LocationAlreadyExists.new(name))
2✔
159
    end
160

161
    def make_sure_not_setting_name_of_multiple_locations!
1✔
162
      raise(TryingToSetMultipleLocationsToSameName.new) \
×
163
        if query.num_results > 1
6✔
164
    end
165
  end
166
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc