Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #26

Merged
merged 2 commits into from
Sep 3, 2024
Merged

Dev #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
16 changes: 12 additions & 4 deletions app/models/rails_xapi/activity_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ def moreInfo=(value)
end

def extensions=(extensions_data)
extensions_data.each do |iri, data|
extension = extensions.build(iri: iri)
extension.value = serialized_value(data)
extensions << extension
unless extensions_data.is_a?(Hash)
raise RailsXapi::Errors::XapiError, I18n.t("rails_xapi.errors.attribute_must_be_a_hash", name: "extensions")
end

# Find any existing extension for the given activity definition.
exts = extensions.where(extendable_type: self.class, extendable_id: id)
# If none, build and save the extension
if exts.blank? && extensions.blank?
extensions_data.each do |iri, data|
p data
extensions.build(iri: iri, value: serialized_value(data))
end
end
end

Expand Down
8 changes: 5 additions & 3 deletions app/models/rails_xapi/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ def statement=(value)
end

def extensions=(extensions_data)
unless extensions_data.is_a?(Hash)
raise RailsXapi::Errors::XapiError, I18n.t("rails_xapi.errors.attribute_must_be_a_hash", name: "extensions")
end

extensions_data.each do |iri, data|
extension = extensions.build(iri: iri)
extension.value = serialized_value(data)
extensions << extension
extensions.build(iri: iri, value: serialized_value(data))
end
end

Expand Down
6 changes: 0 additions & 6 deletions app/models/rails_xapi/context_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ class RailsXapi::ContextActivity < ApplicationRecord
belongs_to :object, class_name: "RailsXapi::Object"

validates :activity_type, presence: true, inclusion: {in: ["parent", "grouping", "category", "other"]}

def type=(value)
# We store the `type` attribute into `activity_type` column to avoid
# reserved key-words issues.
self.activity_type = value
end
end

# == Schema Information
Expand Down
11 changes: 5 additions & 6 deletions app/models/rails_xapi/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
class RailsXapi::Extension < ApplicationRecord
belongs_to :extendable, polymorphic: true

def data=(attributes)
if attributes.is_a?(Hash) && attributes.keys.length == 1
self.iri = attributes.keys.first.to_s
self.value = attributes.values.first.to_s
else
raise ArgumentError, "Invalid data format for Extension"
after_initialize :validate_data

def validate_data
unless attributes.is_a?(Hash)
raise RailsXapi::Errors::XapiError, I18n.t("rails_xapi.errors.attribute_must_be_a_hash", name: "extensions")
end
end
end
Expand Down
13 changes: 5 additions & 8 deletions app/models/rails_xapi/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ def duration_in_seconds=(value)
end

def extensions=(extensions_data)
unless extensions_data.is_a?(Hash)
raise RailsXapi::Errors::XapiError, I18n.t("rails_xapi.errors.attribute_must_be_a_hash", name: "extensions")
end

extensions_data.each do |iri, data|
extension = extensions.build(iri: iri)
extension.value = serialized_value(data)
extensions << extension
extensions.build(iri: iri, value: serialized_value(data))
end
end

Expand Down Expand Up @@ -70,11 +72,6 @@ def validate_score(value)
raise RailsXapi::Errors::XapiError, I18n.t("rails_xapi.errors.invalid_score_value",
value: I18n.t("rails_xapi.validations.score.min"))
end

if min_value.present? && max_value && max_value <= min_value
raise RailsXapi::Errors::XapiError, I18n.t("rails_xapi.errors.invalid_score_value",
value: I18n.t("rails_xapi.validations.score.max"))
end
end

# Validates the duration accordign to the specifications.
Expand Down
8 changes: 4 additions & 4 deletions config/locales/rails_xapi.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ en:
rails_xapi:
xapi_statement: "xAPI statement"
errors:
actor_ifi_must_be_present: "At least one of mbox, mbox_sha1sum, openid, or account must be present"
couldnt_create_the_substatement: "Couldn’t create the substatement"
actor_ifi_must_be_present: "at least one of mbox, mbox_sha1sum, openid, or account must be present"
attribute_must_be_a_hash: "attribute %{name} must be a hash"
couldnt_create_the_substatement: "couldn’t create the substatement"
definition_description_invalid_keys: "invalid keys in definition description: %{values}"
definition_description_must_be_language_map: "definition description must be a language map"
invalid_actor: "invalid actor"
Expand Down Expand Up @@ -31,7 +32,6 @@ en:
wrong_attribute_type: "wrong attribute type for %{name}: %{value}"
validations:
score:
min: "min must be a decimal number less than max"
max: "max must be a decimal number greater than min"
min: "min must be a decimal number lower than max"
raw: "raw must be a decimal between min and max inclusive"
scaled: "scaled must be a decimal number between -1 and 1 inclusive"
8 changes: 4 additions & 4 deletions config/locales/rails_xapi.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ fr:
rails_xapi:
xapi_statement: "déclaration xAPI"
errors:
actor_ifi_must_be_present: "Au moins mbox, mbox_sha1sum, openid, ou account doit être présent"
couldnt_create_the_substatement: "La sous-déclaration n'a pas pu être créée"
actor_ifi_must_be_present: "au moins mbox, mbox_sha1sum, openid, ou account doit être présent"
attribute_must_be_a_hash: "l'attribut %{name} doit être un hash"
couldnt_create_the_substatement: "la sous-déclaration n'a pas pu être créée"
definition_description_invalid_keys: "clés incorrectes dans la description de définition : %{values}"
definition_description_must_be_language_map: "la description de définitionde doit être de type language map"
invalid_actor: "acteur invalide"
Expand Down Expand Up @@ -31,7 +32,6 @@ fr:
wrong_attribute_type: "type d'attribut incorrect pour %{name} : %{value}"
validations:
score:
min: "min doit être un nombre décimal inférieur max"
max: "max doit être un nombre décimal supérieur à min"
min: "min doit être un nombre décimal inférieur à max"
raw: "raw doit être un nombre décimal compris entre min et max inclus"
scaled: "scaled doit être un nombre décimal compris entre -1 and 1 inclus"
6 changes: 3 additions & 3 deletions rails_xapi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Gem::Specification.new do |spec|
spec.version = RailsXapi::VERSION
spec.authors = ["Hipjea"]
spec.email = ["pierre.duverneix@gmail.com"]
spec.homepage = "https://github.com/fondation-unit/rails-xapi"
spec.homepage = "https://github.com/Hipjea/rails-xapi"
spec.summary = "Summary of RailsXapi."
spec.description = "Description of RailsXapi."
spec.license = "MIT"
Expand All @@ -15,8 +15,8 @@ Gem::Specification.new do |spec|
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/fondation-unit/rails-xapi"
spec.metadata["changelog_uri"] = "https://github.com/fondation-unit/rails-xapi/CHANGELOG.md"
spec.metadata["source_code_uri"] = "https://github.com/Hipjea/rails-xapi"
spec.metadata["changelog_uri"] = "https://github.com/Hipjea/rails-xapi/CHANGELOG.md"

spec.files = Dir.chdir(File.expand_path(__dir__)) do
Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
Expand Down
40 changes: 40 additions & 0 deletions spec/models/rails_xapi/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,52 @@
name: "object definition",
description: {"en" => "Object definition"},
type: "Activity",
extensions: {
"http://example.com/profiles/meetings/activitydefinitionextensions/room": {
"name": "Kilby",
"id": "http://example.com/rooms/342"
}
},
moreInfo: "http://example.com/more_infos"
}
))

expect(object.valid?).to be_truthy
end

it "should update an object definition" do
object = RailsXapi::Object.new(@base_object.merge(
definition: {
name: "object definition",
description: {"en" => "Object definition"},
type: "Activity"
}
))

object.save!
object.update_definition({
name: "object updated definition",
description: {"en" => "Object updated definition"},
type: "Activity"
})

expect(object.valid?).to be_truthy
expect(object.definition.name).to eq("object updated definition")
end

it "should raise an error with incorrect extensions" do
object = @base_object.merge(
definition: {
name: "object definition",
extensions: "http://example.com/profiles/meetings/activitydefinitionextensions/room",
}
)

expect { RailsXapi::Object.new(object) }.to raise_error do |error|
expect(error).to be_a(RailsXapi::Errors::XapiError)
expect(error.message).to eq I18n.t("rails_xapi.errors.attribute_must_be_a_hash", name: "extensions")
end
end
end

# == Schema Information
Expand Down
70 changes: 70 additions & 0 deletions spec/models/rails_xapi/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,76 @@
expect(error.message).to eq 'Invalid ISO 8601 duration: "IncorrectDuration"'
end
end

it "should not be valid with an incorrect scaled value" do
result = {
score: {
raw: 1,
min: 2,
max: 10,
scaled: -1.1
},
statement: RailsXapi::Statement.new(@default_statement)
}

expect { RailsXapi::Result.new(result) }.to raise_error do |error|
expect(error).to be_a(RailsXapi::Errors::XapiError)
expect(error.message).to eq I18n.t("rails_xapi.errors.invalid_score_value",
value: I18n.t("rails_xapi.validations.score.scaled"))
end
end

it "should not be valid with a min value greater than max" do
result = {
score: {
min: 10,
max: 2
},
statement: RailsXapi::Statement.new(@default_statement)
}

expect { RailsXapi::Result.new(result) }.to raise_error do |error|
expect(error).to be_a(RailsXapi::Errors::XapiError)
expect(error.message).to eq I18n.t("rails_xapi.errors.invalid_score_value",
value: I18n.t("rails_xapi.validations.score.min"))
end
end

it "should have a boolean completion value" do
completion_val = "yes"
result = {
completion: false,
statement: RailsXapi::Statement.new(@default_statement)
}

result_object = RailsXapi::Result.new(result)
result_object.completion = completion_val
result_object.save!

expect(result_object.completion).to eq(true)
end

it "should set the duration in iso8601 from seconds" do
result = RailsXapi::Result.new(
duration_in_seconds: 120,
statement: RailsXapi::Statement.new(@default_statement)
)

expect(result.valid?).to be_truthy
expect(result.duration).to eq("PT2M")
end

it "should not be a valid extension" do
result = {
extensions: "http://example.com/extension/1",
statement: RailsXapi::Statement.new(@default_statement)
}

expect { RailsXapi::Result.new(result) }.to raise_error do |error|
expect(error).to be_a(RailsXapi::Errors::XapiError)
expect(error.message).to eq I18n.t("rails_xapi.errors.attribute_must_be_a_hash", name: "extensions")
end
end
end

# == Schema Information
Expand Down
33 changes: 32 additions & 1 deletion spec/models/rails_xapi/verb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,43 @@
expect(verb.valid?).to be_truthy
end

it "should not be valid with an incorrect language map key" do
verb_data = @base_verb.merge(
display: {
"e" => "Example"
}
)
verb = RailsXapi::Verb.new(verb_data)

expect { verb.save! }.to raise_error do |error|
expect(error).to be_a(RailsXapi::Errors::XapiError)
expect(error.message).to eq I18n.t("rails_xapi.errors.definition_description_invalid_keys", values: "e")
end
end

it "should automatically set the display value" do
verb = RailsXapi::Verb.new(@base_verb)
verb.save
verb.save!

expect(verb.display).to_not be_nil
end

it "should display the correct hash value" do
verb = RailsXapi::Verb.new(@base_verb)
verb.save!

expect(verb.to_locale).to eq(RailsXapi::Verb::VERBS_LIST.values[0])
end

it "should raise an exception if no display value" do
verb = RailsXapi::Verb.new(id: "http://example.com/verbs/not-in-the-list")
verb.display = nil

expect { verb.save! }.to raise_error do |error|
expect(error).to be_a(RailsXapi::Errors::XapiError)
expect(error.message).to eq I18n.t("rails_xapi.errors.missing_verb_display")
end
end
end

# == Schema Information
Expand Down