Skip to content

Commit

Permalink
storing screen resolution, calculating aspect ratio [concerto#131 sta…
Browse files Browse the repository at this point in the history
…te:open assigned:'Zach Rowe'] [concerto#111 state:open assigned:'Zach Rowe']
  • Loading branch information
zr2d2 committed Apr 10, 2011
1 parent 2bfe146 commit 69043b3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
21 changes: 21 additions & 0 deletions app/models/screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,26 @@ class Screen < ActiveRecord::Base
#For now, the check will be string based, it should probably be moved to
#something like if owner_type.is_class (however that would work)
validates :owner, :presence => true, :associated => true, :if => Proc.new { ["User", "Group"].include?(owner_type) }
validates :width, :presence => true
validates :height, :presence => true

# Determine the screen's aspect ratio. If it doesn't exist, calculate it
def aspect_ratio
if width.nil? || height.nil?
return { "width", "", "height", "" }
end
gcd = gcd(width,height)
aspect_width = width/gcd
aspect_height = height/gcd
return { "width", aspect_width, "height", aspect_height }
end

# Run Euclidean algorithm to find GCD
def gcd (a,b)
if b == 0
return a
end
return gcd (b, a.modulo(b) )
end
end

12 changes: 12 additions & 0 deletions app/views/screens/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@
<%= f.collection_select :template_id, @templates, :id, :name %>
</div>
</div>
<div class="field">
<%= f.label :width %>
<div class="inputs">
<%= f.text_field :width %>
</div>
</div>
<div class="field">
<%= f.label :height %>
<div class="inputs">
<%= f.text_field :height %>
</div>
</div>
</div>

<div class="submit_bar actions">
Expand Down
5 changes: 4 additions & 1 deletion app/views/screens/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<b>Template:</b>
<%= @screen.template.name %>
</p>

<p>
<b>resolution</b>
<%= @screen.width %> X <%= @screen.height %> (<%= @screen.aspect_ratio["width"] %>:<%= @screen.aspect_ratio["height"] %>)
</p>

<%= link_to 'Edit', edit_screen_path(@screen) %> |
<%= link_to 'Back', screens_path %>
11 changes: 11 additions & 0 deletions db/migrate/20110317045358_add_resolution_to_screens.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddResolutionToScreens < ActiveRecord::Migration
def self.up
add_column :screens, :width, :int
add_column :screens, :height, :int
end

def self.down
remove_column :screens, :height
remove_column :screens, :width
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110317033903) do
ActiveRecord::Schema.define(:version => 20110317045358) do

create_table "contents", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -94,6 +94,8 @@
t.integer "template_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "width"
t.integer "height"
end

create_table "submissions", :force => true do |t|
Expand Down

0 comments on commit 69043b3

Please sign in to comment.