Skip to content

Instantly share code, notes, and snippets.

# POST
def check_assignation
@inventory_ids = params[:inv]
@inventories = OleCore::Inventory.includes(:course, account: {subscription: :subscription_plan})
.where(id: params[:inv])
prevent_empty_trainees_and_courses
# If this assignation doesn't require addons, no confirmation required either.
if !CourseAssigner.requires_addons?(@inventories, @checked_users, current_user)
@laspluviosillas
laspluviosillas / offerings.rb
Created January 20, 2014 21:37
Example of retrieving offerings.
Spree::LineItem.class_eval do
def offering
variant.offering(self.qty)
end
end
Spree::Variant.class_eval do
# has_many :volume_prices (viene de la gema)
has_many :offerings
Failures:
1) Spree::Admin::VariantsController POST Create a new variant with associated course
Failure/Error: Spree::Variant.all.count.should eq(2)
expected: 2
got: 1
(compared using ==)
# ./spec/controllers/spree/admin/variants_controller_spec.rb:26:in `block (3 levels) in <top (required)>'
class Search
def initialize(searchable)
@searchable = searchable
end
def starts_with(letter, attribute)
@searchable.where(attribute=> 'letter') # would be some variant of a LIKE query.
end
end
class ContentProviderCalculator
def calculate(commissionable)
olei = # find olei content provider
olec = # find olec content provider
content_amount = commissionable.subtotal * .2 # 20% goes to content providers
if commissionable.premium?
# create commission for olei of 66.7% of content_amount
# create commission for olec of 33.3% of content_amount
else
def update
# fetch @conversation having its rank updated.
#
conversations = @discussion.conversations.order(:rank).to_a
# Move conversation to new position by inserting in array at rank and deleting it from previous position.
conversations.insert(@conversation.rank, conversations.delete_at(@conversation))
# Update all conversation ranks to correspond to new array order.
# Very inefficient as this uses a n+1 query. You could optimize to use update_all
class ResultSetsController < ApplicationController
def index
@survey = Survey.find(params[:id]) # should be :survey_id if you want to conform to rails standard.
@quiz = @survey.quiz # try to retrieve quiz from survey, you should not have to load all the result sets to find the right quiz id.
@result_sets = Survey.result_sets.owned_by(result_set_owner)
# Rest of code follows below but requires major refactoring.
end
private
class Programme < AR::Base
has_many :villages
has_many :conversations, though: :villages
end
class Village < AR::Base
has_many :discussions
has_many :conversations, through: :discussions
end
class Change < ActiveRecord::Base
has_many :conversations
validates :name, presence: true
def self.top_ranked(load_conversation=true)
with_rank(load_conversation).order("conversations_rank desc").limit(1)
end
def self.with_rank(load_conversation=true)
class Village < AR::Base
has_many :discussions
has_many :conversations, through: :discussions
has_many :changes, through: :conversations
def top_change
changes.merge Change.top_ranked(false)
end
end