Permalink
Browse files

added various models and their tests

  • Loading branch information...
1 parent 10db46f commit 8f9b43ed358bc5b010d851499c78f397908f8968 @nikhgupta committed Jan 12, 2014
View
@@ -44,6 +44,9 @@ end
# Use debugger
# gem 'debugger', group: [:development, :test]
+gem 'acts-as-taggable-on'
+gem 'validates_timeliness'
+
group :development do
gem 'pry-rails'
gem 'shotgun'
View
@@ -25,6 +25,8 @@ GEM
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
+ acts-as-taggable-on (2.4.1)
+ rails (>= 3, < 5)
addressable (2.3.5)
arel (4.0.1)
atomic (1.1.14)
@@ -204,6 +206,7 @@ GEM
thread_safe (0.1.3)
atomic
tilt (1.4.1)
+ timeliness (0.3.7)
timers (1.1.0)
tins (0.13.1)
treetop (1.4.15)
@@ -215,6 +218,8 @@ GEM
uglifier (2.3.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
+ validates_timeliness (3.0.14)
+ timeliness (~> 0.3.6)
websocket-driver (0.3.1)
xpath (2.0.0)
nokogiri (~> 1.3)
@@ -225,6 +230,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ acts-as-taggable-on
coffee-rails (~> 4.0.0)
coveralls
cucumber-rails
@@ -250,4 +256,5 @@ DEPENDENCIES
terminal-notifier-guard
turbolinks
uglifier (>= 1.3.0)
+ validates_timeliness
zeus
View
@@ -0,0 +1,28 @@
+class Channel < ActiveRecord::Base
+
+ # associations
+ belongs_to :slide
+
+ # validations
+ validates :url, presence: true, url: true, allow_blank: false
+ validates_presence_of :slide
+
+ # on save
+ before_save :sanitize_data
+
+ def timeline_data
+ data = { media: url }
+ data[:thumbnail] = thumbnail if thumbnail
+ data[:credit] = credit if credit
+ data[:caption] = caption if caption
+ data
+ end
+
+ private
+
+ def sanitize_data
+ self.thumbnail = nil if self.thumbnail.blank?
+ self.caption = nil if self.caption.blank?
+ self.credit = nil if self.credit.blank?
+ end
+end
View
@@ -0,0 +1,37 @@
+class Event < ActiveRecord::Base
+
+ # associations
+ has_many :slides
+ belongs_to :language
+ belongs_to :map_type
+ belongs_to :font
+
+ # validations
+ validates :title, presence: true, allow_blank: false
+
+ # on save
+ before_save :sanitize_data
+
+ def cover
+ slides.where(cover: true).first
+ end
+
+ def timeline_data
+ return nil if self.slides.empty?
+ _slides = self.slides.where(cover: false).map(&:timeline_data)
+ { timeline: self.cover.timeline_data.merge(type: "default", date: _slides) }
+ end
+
+ def timeline_json
+ data = self.timeline_data
+ data ||= { timeline: { headline: "No Slides Found :(" } }
+ data.to_json
+ end
+
+ private
+
+ def sanitize_data
+ self.description = nil if self.description.blank?
+ end
+
+end
View
@@ -0,0 +1,4 @@
+class Font < ActiveRecord::Base
+ # associations
+ has_many :events
+end
View
@@ -0,0 +1,4 @@
+class Language < ActiveRecord::Base
+ # associations
+ has_many :events
+end
View
@@ -0,0 +1,4 @@
+class MapType < ActiveRecord::Base
+ # associations
+ has_many :events
+end
View
@@ -0,0 +1,39 @@
+class Slide < ActiveRecord::Base
+
+ # associations
+ belongs_to :event
+ has_one :channel
+
+ # validations
+ validates :title, presence: true, allow_blank: false
+ validates_presence_of :event
+ validates_datetime :end_date, if: :start_date, allow_nil: true, on_or_after: :start_date
+ validates_datetime :end_date, unless: :start_date, allow_nil: true, is_at: Time.at(0)
+ validates_datetime :start_date, if: :end_date, allow_nil: false, on_or_before: :end_date
+ validates_datetime :start_date, unless: :end_date, allow_nil: true, on_or_before: lambda{|m| Time.now}
+
+ # behaviours
+ acts_as_taggable
+
+ # on save
+ before_save :sanitize_data
+
+ def timeline_data
+ data = { headline: title }
+ data[:text] = description if description
+ data[:tag] = tag_list.join(", ") if tag_list.any?
+ data[:startDate] = start_date.strftime("%Y,%m,%d") if start_date
+ data[:endDate] = end_date.strftime("%Y,%m,%d") if end_date
+ data[:classname] = class_name if class_name
+ data[:asset] = channel.timeline_data if channel
+ data
+ end
+
+ private
+
+ def sanitize_data
+ self.cover = true unless self.event.cover
+ self.class_name = nil if self.class_name.blank?
+ self.description = nil if self.description.blank?
+ end
+end
@@ -0,0 +1,13 @@
+class UrlValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ begin
+ uri = URI.parse(value)
+ resp = uri.kind_of?(URI::HTTP)
+ rescue URI::InvalidURIError
+ resp = false
+ end
+ unless resp == true
+ record.errors[attribute] << (options[:message] || "is not an url")
+ end
+ end
+end
@@ -0,0 +1,40 @@
+ValidatesTimeliness.setup do |config|
+ # Extend ORM/ODMs for full support (:active_record, :mongoid).
+ # config.extend_orms = [ :active_record ]
+ #
+ # Default timezone
+ # config.default_timezone = :utc
+ #
+ # Set the dummy date part for a time type values.
+ # config.dummy_date_for_time_type = [ 2000, 1, 1 ]
+ #
+ # Ignore errors when restriction options are evaluated
+ # config.ignore_restriction_errors = false
+ #
+ # Re-display invalid values in date/time selects
+ # config.enable_date_time_select_extension!
+ #
+ # Handle multiparameter date/time values strictly
+ # config.enable_multiparameter_extension!
+ #
+ # Shorthand date and time symbols for restrictions
+ # config.restriction_shorthand_symbols.update(
+ # :now => lambda { Time.current },
+ # :today => lambda { Date.current }
+ # )
+ #
+ # Use the plugin date/time parser which is stricter and extendable
+ # config.use_plugin_parser = false
+ #
+ # Add one or more formats making them valid. e.g. add_formats(:date, 'd(st|rd|th) of mmm, yyyy')
+ # config.parser.add_formats()
+ #
+ # Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
+ # config.parser.remove_formats()
+ #
+ # Change the amiguous year threshold when parsing a 2 digit year
+ # config.parser.ambiguous_year_threshold = 30
+ #
+ # Treat ambiguous dates, such as 01/02/1950, as a Non-US date.
+ # config.parser.remove_us_formats
+end
@@ -0,0 +1,16 @@
+en:
+ errors:
+ messages:
+ invalid_date: "is not a valid date"
+ invalid_time: "is not a valid time"
+ invalid_datetime: "is not a valid datetime"
+ is_at: "must be at %{restriction}"
+ before: "must be before %{restriction}"
+ on_or_before: "must be on or before %{restriction}"
+ after: "must be after %{restriction}"
+ on_or_after: "must be on or after %{restriction}"
+ validates_timeliness:
+ error_value_formats:
+ date: '%Y-%m-%d'
+ time: '%H:%M:%S'
+ datetime: '%Y-%m-%d %H:%M:%S'
@@ -0,0 +1,15 @@
+class CreateSlides < ActiveRecord::Migration
+ def change
+ create_table :slides do |t|
+ t.integer :event_id
+ t.string :title, null: false
+ t.text :description, null: true
+ t.datetime :start_date, null: true
+ t.datetime :end_date, null: true
+ t.boolean :cover, default: false
+ t.string :class_name, null: true
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,13 @@
+class CreateChannels < ActiveRecord::Migration
+ def change
+ create_table :channels do |t|
+ t.integer :slide_id
+ t.text :url, null: false
+ t.text :thumbnail, null: true
+ t.text :caption, null: true
+ t.string :credit, null: true
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,10 @@
+class CreateLanguages < ActiveRecord::Migration
+ def change
+ create_table :languages do |t|
+ t.string :code
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,10 @@
+class CreateMapTypes < ActiveRecord::Migration
+ def change
+ create_table :map_types do |t|
+ t.string :name
+ t.string :kind
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,9 @@
+class CreateFonts < ActiveRecord::Migration
+ def change
+ create_table :fonts do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,18 @@
+class CreateEvents < ActiveRecord::Migration
+ def change
+ create_table :events do |t|
+ t.string :title, null: false
+ t.text :description, null: true
+ t.integer :language_id, default: 9
+ t.integer :map_type_id, default: 4
+ t.integer :font_id, default: 4
+ t.boolean :start_at_end, default: false
+ t.integer :start_at_slide, default: 0
+ t.integer :start_zoom_adjust, default: 0
+ t.boolean :hash_bookmark, default: true
+ t.boolean :debug, default: false
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,30 @@
+class ActsAsTaggableOnMigration < ActiveRecord::Migration
+ def self.up
+ create_table :tags do |t|
+ t.string :name
+ end
+
+ create_table :taggings do |t|
+ t.references :tag
+
+ # You should make sure that the column created is
+ # long enough to store the required class names.
+ t.references :taggable, :polymorphic => true
+ t.references :tagger, :polymorphic => true
+
+ # Limit is created to prevent MySQL error on index
+ # length for MyISAM table type: http://bit.ly/vgW2Ql
+ t.string :context, :limit => 128
+
+ t.datetime :created_at
+ end
+
+ add_index :taggings, :tag_id
+ add_index :taggings, [:taggable_id, :taggable_type, :context]
+ end
+
+ def self.down
+ drop_table :taggings
+ drop_table :tags
+ end
+end
Oops, something went wrong.

0 comments on commit 8f9b43e

Please sign in to comment.