lib/doggy/cli/edit.rb in doggy-2.0.29 vs lib/doggy/cli/edit.rb in doggy-2.0.30
- old
+ new
@@ -7,64 +7,30 @@
@param = param
end
def run
resource = resource_by_param
- resource.read_only = false
return Doggy.ui.error("Could not find resource with #{ @param }") unless resource
- Dir.chdir(File.dirname(resource.path)) do
- if resource.class.to_s.downcase =~ /dashboard/
- forked_resource = fork(resource)
- system("open '#{ forked_resource.human_edit_url }'")
+ forked_resource = fork(resource)
+ system("open '#{ forked_resource.human_edit_url }'")
+ wait_for_edit
- wait_for_edit
-
- new_resource = Doggy::Models::Dashboard.find(forked_resource.id)
- new_resource.id = resource.id
- new_resource.title = resource.title
- new_resource.description = resource.description
- new_resource.path = resource.path
- new_resource.save_local
-
- forked_resource.destroy
- elsif resource.class.to_s.downcase =~ /screen/
- forked_resource = fork(resource)
- system("open '#{ forked_resource.human_edit_url }'")
-
- wait_for_edit
-
- new_resource = Doggy::Models::Screen.find(forked_resource.id)
- new_resource.id = resource.id
- new_resource.board_title = resource.board_title
- new_resource.path = resource.path
- new_resource.save_local
-
- forked_resource.destroy
- elsif resource.class.to_s.downcase =~ /monitor/
- forked_resource = fork(resource)
- system("open '#{ forked_resource.human_edit_url }'")
-
- wait_for_edit
-
- new_resource = Doggy::Models::Monitor.find(forked_resource.id)
- new_resource.id = resource.id
- new_resource.name = resource.name
- new_resource.path = resource.path
- new_resource.save_local
-
- forked_resource.destroy
- else
- system("open '#{ resource.human_edit_url }'")
-
- wait_for_edit
-
- new_resource = resource.class.find(resource.id)
- new_resource.path = resource.path
- new_resource.save_local
- end
+ new_resource = Doggy::Model.infer_type(resource.attributes).find(forked_resource.id)
+ new_resource.id = resource.id
+ if new_resource.is_a?(Doggy::Models::Dashboard)
+ new_resource.title = resource.title
+ new_resource.description = resource.description
+ elsif new_resource.is_a?(Doggy::Models::Monitor)
+ new_resource.name = resource.name
+ elsif new_resource.is_a?(Doggy::Models::Screen)
+ new_resource.board_title = resource.board_title
end
+ new_resource.path = resource.path
+ new_resource.save_local
+
+ forked_resource.destroy
end
private
def resource_by_param
@@ -87,20 +53,20 @@
return resources.find { |res| res.path == full_path }
end
end
def wait_for_edit
- while !Doggy.ui.yes?('Are you done editing?') do
+ while !Doggy.ui.yes?('Are you done editing?(Y/N)') do
Doggy.ui.say "run, rabbit run / dig that hole, forget the sun / and when at last the work is done / don't sit down / it's time to dig another one"
end
end
def fork(resource)
- salt = (0...12).map { (65 + rand(26)).chr.downcase }.join
-
+ salt = random_word
forked_resource = resource.dup
forked_resource.id = nil
+ forked_resource.refute_read_only!
if resource.class.to_s.downcase =~ /dashboard/
forked_resource.title = "[#{ salt }] " + forked_resource.title
forked_resource.description = "[fork of #{ resource.id }] " + forked_resource.title
elsif resource.class.to_s.downcase =~ /screen/
forked_resource.board_title = "[#{ salt }] " + forked_resource.board_title
@@ -109,9 +75,13 @@
else
raise StandardError.new('Unknown resource type, cannot edit.')
end
forked_resource.save
forked_resource
+ end
+
+ def random_word
+ (0...12).map { (65 + rand(26)).chr.downcase }.join
end
end
end